Sunday, June 24, 2007

use "retain statement" to make a count variable

**This code comes from UCLA statistical computing seminar
(link here to code)

proc sort data=kids out=sort_kids;
by famid;
run;
data retain1;
set sort_kids;
retain sumwt count;

/*carry over the value from previous obs to next obs*/
by famid;
if first.famid then do;
/*at 1st obs of each family set sumwt and count = 0*/
sumwt=0;
count=0;
end;
sumwt = sumwt + wt;
count = count + 1;
meanwt = sumwt/count;
run;
proc print data=retain1;
var famid kidname wt sumwt count meanwt;
run;