Say I have the following dataset:
data test;
input id flag1 flag2 flag3 flag4 date;
datalines;
A 0 0 1 0 20170401
A 1 0 0 0 20170402
A 1 0 0 0 20170403
A 0 0 0 1 20170405
A 0 1 0 0 20170406
A 1 0 0 0 20170407
A 0 1 0 0 20170407
B 1 0 0 0 20170402
B 1 0 0 0 20170403
C 0 0 1 0 20170404
C 0 1 0 0 20170404
C 0 1 0 0 20170404
C 1 0 0 0 20170405
D 0 0 0 0 20170405
;
run;
If I wanted to create an output that counts the number of unique dates each flag pops up on for each ID, what would be the best way of doing it, other than creating a separate intermediate dataset for each count and then combining it at the end? My initial thought had been to use a CASE WHEN and COUNT(DISTINCT DATE) in PROC SQL, but that just gave me a total count on the lines where a particular flag equaled 1.
Desired outcome:
ID count_flag1 count_flag2 count_flag3 count_flag_4
A 3 2 1 1
B 2 0 0 0
C 1 1 1 0
D 0 0 0 0
A few sets of variables can handle this, with one pass through the data. Assuming your data set is sorted BY ID DATE:
data want;
set have;
by id date;
if first.id then do;
count_flag1=0; count_flag2=0; count_flag3=0; count_flag4=0;
end;
if first.date then do;
max_flag1=0; max_flag2=0; max_flag3=0; max_flag4=0;
end;
retain max_flag: ;
max_flag1 = max(flag1, max_flag1) ;
max_flag2 = max(flag2, max_flag2) ;
max_flag3 = max(flag3, max_flag3) ;
max_flag4 = max(flag4, max_flag4) ;
if last.date;
count_flag1 + max_flag1;
count_flag2 + max_flag2;
count_flag3 + max_flag3;
count_flag4 + max_flag4;
if last.id;
drop max_flag: flag: ;
run;
Note that the COUNT_FLAG variables are automatically retained ... that's one effect of the statements that look like:
count_flag1 + max_flag1;
You can use proc summary (or proc means) to get what you want:
proc summary data=have;
class ID;
var flag1-flag4;
output out=want(rename=(
flag1=count_flag1 flag2=count_flag2
flag3=count_flag3 flag4=count_flag4))
sum=;
run;
or you can do it by sql:
proc sql;
create table wand as
select id,
sum(flag1) as count_flag1,
sum(flag2) as count_flag2,
sum(flag3) as count_flag3,
sum(flag4) as count_flag4
from have
groupe by ID;
quit;
Both of those miss a crucial part of the question: the count needs to be of distinct dates for each flags. You'll note if you look at the line for ID C that count_flag2 should be 1, not 2.
A few sets of variables can handle this, with one pass through the data. Assuming your data set is sorted BY ID DATE:
data want;
set have;
by id date;
if first.id then do;
count_flag1=0; count_flag2=0; count_flag3=0; count_flag4=0;
end;
if first.date then do;
max_flag1=0; max_flag2=0; max_flag3=0; max_flag4=0;
end;
retain max_flag: ;
max_flag1 = max(flag1, max_flag1) ;
max_flag2 = max(flag2, max_flag2) ;
max_flag3 = max(flag3, max_flag3) ;
max_flag4 = max(flag4, max_flag4) ;
if last.date;
count_flag1 + max_flag1;
count_flag2 + max_flag2;
count_flag3 + max_flag3;
count_flag4 + max_flag4;
if last.id;
drop max_flag: flag: ;
run;
Note that the COUNT_FLAG variables are automatically retained ... that's one effect of the statements that look like:
count_flag1 + max_flag1;
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.