Count of multiple flags based on unique dates and IDs

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Count of multiple flags based on unique dates and IDs

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


Accepted Solutions
Solution
‎07-27-2017 03:26 PM
Super User
Posts: 5,509

Re: Count of multiple flags based on unique dates and IDs

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;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,568

Re: Count of multiple flags based on unique dates and IDs

[ Edited ]

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;
Contributor
Posts: 24

Re: Count of multiple flags based on unique dates and IDs

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.

Solution
‎07-27-2017 03:26 PM
Super User
Posts: 5,509

Re: Count of multiple flags based on unique dates and IDs

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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 164 views
  • 1 like
  • 3 in conversation