Hi,
I have a dataset at the person-day level (that is, each row is one day for one person). Each person in the dataset appears in multiple different rows. I need to be able to calculate scores for each person based on their values of other variables.
For example:
ID Date Var3 Var4
1 5/1/14 a d
2 6/1/14 b d
1 5/15/14 c e
1 5/31/14 a d
3 4/1/13 a f
I would want to calculate one score by seeing how many values of "a" each person had for Var3 over time, and another by seeing how many values of "d" each person had for Var4 over time. '
Any help is much appreciated.
This is what you want?
data want;
set have;
by id;
retain a_count d_count temp;
if first.id then do;
call missing(a_count,d_count,temp);
temp=var5;
end;
a_count+ifc(var3='a' and temp^=var5,1,0);
d_count+ifc(var4='d',1,0);
temp=var5;
if last.id then output;
drop temp;
run;
proc sql;
create table want as
select
id,
sum(var3='a') as a_count,
sum(var4='d') as d_count
from
have
group by id;
quit;
data step
proc sort data=have;
by id;
run;
data want;
set have;
by id;
retain a_count d_count;
if first.id then call missing(a_count,d_count);
a_count+ifc(var3='a',1,0);
d_count+ifc(var4='d',1,0);
if last.id then output;
run;
If I use ifc, can I add additional conditions (for example, can I count values of "a" for var3 only if the value of var5 is different from the previous row)?
This is what you want?
data want;
set have;
by id;
retain a_count d_count temp;
if first.id then do;
call missing(a_count,d_count,temp);
temp=var5;
end;
a_count+ifc(var3='a' and temp^=var5,1,0);
d_count+ifc(var4='d',1,0);
temp=var5;
if last.id then output;
drop temp;
run;
Proc Freq is built for counting ( you get the output into tables via ODS):
proc freq data=have;
table id*(var3 var4) /nocol nocum norow nopercent;
run;
Regards,
Haikuo
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.