01-04-2017 07:36 PM
I wonder how to code the proc means to calculate the avarage of the values by id and visit.
Thanks in advance.
01-04-2017 08:07 PM
Try proc sql
data have; input id $ visit value ; cards; a 1 10 a 1 20 a 1 30 a 2 50 a 2 30 b 1 10 b 1 20 ; proc sql; create table test as select *, mean(value) as average from have group by id ,visit; quit;
01-04-2017 08:18 PM
we could also get the same via data step
data want; do until(last.visit); set have; by id visit; if first.visit then do; s=value; c=1; end; else do; s+value; c+1; end; average=s/c; end; do until(last.visit); set have; by id visit; output; end; drop s c; run;
01-04-2017 11:12 PM
Since you originally asked about PROC MEANS, here is what it would look like:
proc means data=have nway noprint;
class id visit;
output out=want (drop=_type_ _freq_) mean=average;
Need further help from the community? Please ask a new question.