BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Walternate
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
slchen
Lapis Lazuli | Level 10

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;

View solution in original post

5 REPLIES 5
DBailey
Lapis Lazuli | Level 10

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;

slchen
Lapis Lazuli | Level 10

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;

Walternate
Obsidian | Level 7

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)?

slchen
Lapis Lazuli | Level 10

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;

Haikuo
Onyx | Level 15

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

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 7622 views
  • 6 likes
  • 4 in conversation