We always have this question in mind, while we do the SAS programming and here is the simple answer for that, we just need to use SUM statement and the FIRST.variable in the SET statement and then the RETAIN statement to calculate the observations count per subject.
By doing some minor modification we can calculate observations count per subject per visit also. (Just include visit variable in the BY variable list in PROC sort and First. variable list in datastep with SET statement).
For example:
data dsn;
input patid implants;
datalines;
1 3
1 1
1 2
1 1
2 1
2 2
3 1
4 2
3 1
4 5
2 3
1 6
;
run;
proc sort data=dsn;
by patid;
run;
data dsn1;
set dsn;
by patid;
cnt+1;
if first.patid then cnt=1;
run;
proc sort data=dsn1;
by patid descending cnt;
run;
data dsn2;
set dsn1;
by patid;
retain totcnt;
if first.patid then totcnt=cnt;
output;
run;
By doing some minor modification we can calculate observations count per subject per visit also. (Just include visit variable in the BY variable list in PROC sort and First. variable list in datastep with SET statement).
For example:
data dsn;
input patid implants;
datalines;
1 3
1 1
1 2
1 1
2 1
2 2
3 1
4 2
3 1
4 5
2 3
1 6
;
run;
proc sort data=dsn;
by patid;
run;
data dsn1;
set dsn;
by patid;
cnt+1;
if first.patid then cnt=1;
run;
proc sort data=dsn1;
by patid descending cnt;
run;
data dsn2;
set dsn1;
by patid;
retain totcnt;
if first.patid then totcnt=cnt;
output;
run;
proc print data=dsn2;
run;
Output:
hi
ReplyDeletethere seems some issue in the output you have mentioned. there should be only 5 obs with patid=1.
plz mention if i misunderstood it.
-Karthik
That was a typo error.
ReplyDeleteThanks for attention karthik...
I made changes in the output...
regards
sarath
What if you want to count each observation only if some other field meets a certain criteria (such as not zero)?
ReplyDeleteFor example:
patid num cnt
1 1 1
1 0 1
1 1 2
1 1 3
2 1 1
2 0 1
2 0 1
3 1 1
3 1 2
4 0 0
is you want to count the obs to totcnt only if implant count GT 2...
ReplyDeleteadd like this..
if first.patid and implants GT 2 then totcnt=cnt;
Sarath
We can calculate number of observations per subject in the dataset by using below simplest code
ReplyDeletedata dsn;
input patid implants;
datalines;
1 3
1 1
1 2
1 1
2 1
2 2
3 1
4 2
3 1
4 5
2 3
1 6
;
run;
/***Counting the number of observations per Subject***/
proc sql;
select *,count(patid) as totcnt
from dsn
group by patid
order by patid;
quit;
/****Naveen Boora****/
Why can you use proc freq??
ReplyDeletedata dsn;
input patid implants;
datalines;
1 3
1 1
1 2
1 1
2 1
2 2
3 1
4 2
3 1
4 5
2 3
1 6
;
run;
proc freq data = dsn;
tables patid/ out = dsn1;
run;
how can i count the number of subject in a year in panel data??
ReplyDeletehow do i count the nulber of subjects per year??
ReplyDeleteTable2: Relationship of recipient with donor
ReplyDeletemother, father-:Related
How to retrieve the number of observations from a particular category, can somebody help ?
Example - wife-:Spousal
Other-:Unrelated
Category Number of Records
Related XX
Spousal XX
Unrelated XX
Total XX
Use proc freq .
ReplyDeleteHello,
ReplyDeleteHow I can create a frequency variable? I mean:
proc freq data = dsn;
tables patid/ out = dsn1;
run;
I need dsn1 to be a variable inside my original dataset (dsn) and not another dataset
Thx
try this.....
ReplyDeleteproc sql;
create table dsn as
select *, count(ptid) as count from dsn
group by ptid
;
;
quit;