Monday, October 21, 2013

Counting the number of measures (observations) per subject in a SAS data set

data temp;
    input id num;
datalines;
1 1
1 2
1 1
2 1
2 2
3 1
;
run;

proc sort data=temp;
   by id;
run;

data temp1;
   set temp;
   by id;
   count+1;
   if first.id then count=1;
run;

proc sort;
   by id descending count;
run;

data temp2;
   set temp1;
   by id;
   retain totc;
   if first.id then
     totc=count;
   output;
run;

proc print;
run;


If you run the program above, you will get the following SAS output:


id   num count totc
1     1       1         3
1     2       2         3
1     1       3         3
2     1       1         2
2     2       2         2
3     1       1         1

No comments:

Post a Comment