options ls=120 ps=60; filename foo url "http://www.uvm.edu/~abh/stat295/datasets/bugs.dat"; data bugs; infile foo; input year month day site trap trt @; date = mdy(month,day,year); format date date9.; do species = 1 to 24; input count @ ; if count ne 0 then output; end; run; title 'STAT 295 Final Exam'; title2 'Shannon-Wiener index calculation'; proc sort data=bugs; by date trt site species; run; data species; set bugs; by date trt site species; if first.species then species_totals = 0; species_totals + count; if last.species then output; run; data traps; set species; by date trt site; if first.site then site_totals = 0; site_totals + species_totals; if last.site then output; run; data all_totals; merge traps species; by date trt site; p = species_totals/site_totals; p_logp = p*log(p); run; data shannon; set all_totals; by date trt site; if first.site then h = 0; h + p_logp; if last.site then do; h = -h; output; end; run; proc print data=shannon; var date site h; run;