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;

5 comments:

Jeremy said...

Thank you!

chalewa said...

thanks for this.

i added

if last.varname then output;

to only grab the total of each variable.

Beng said...

you helped me a lot!

Naveen said...

What will be its output look like in saslog???

Unknown said...

Faith is taking the first step even when you don't see the whole staircase. See the link below for more info.


#first
www.ufgop.org