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

I'm not sure of the best approach for this, so I'll provide a current table example and the desired output table. The first table is a series of records. The activity types are known, so I'm thinking we could use WHERE ActivityType = "App_New". But am open to any and all suggestions/help.

 

IDDateActivityType
B123D11/01/2020APP_New
B123D11/01/2020APP_New
A932F11/01/2020APP_Edit
C456E11/01/2020APP_New
B123D11/02/2020APP_Edit
A932F11/02/2020APP_New
C456E11/02/2020APP_Edit
C456E11/02/2020APP_New

 

What I'm looking to accomplish, is to count the number of specific activity types by ID and by Date. So the "new" table would look something like this (see below), based off the above table.

DateIDTotal_App_NewTotal_App_Edit
11/01/2020B123D20
11/01/2020A932F01
11/01/2020C456E01
11/02/2020B123D01
11/02/2020A932F10
11/02/2020C456E11
1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @BlayLay  You could resort to lazy way of doing it by utilizing Proc Freq SPARSE option and a simple transpose. Increasingly I am getting used to these methods-

 

data input;
  input ID $5. Date :mmddyy10. ActivityType $9.;
  format Date date9.;
  datalines;
B123D 11/01/2020 APP_New
B123D 11/01/2020 APP_New
A932F 11/01/2020 APP_Edit
C456E 11/01/2020 APP_New
B123D 11/02/2020 APP_Edit
A932F 11/02/2020 APP_New
C456E 11/02/2020 APP_Edit
C456E 11/02/2020 APP_New
;
run;

proc freq data=input;
 tables date*id*ActivityType/sparse noprint out=temp(drop=percent);
run;

proc transpose data=temp out=want(drop=_:) prefix=Total_;
 by date id;
 var count;
 id ActivityType;
run;

View solution in original post

7 REPLIES 7
GertNissen
Barite | Level 11
data input;
  input ID $5. Date :mmddyy10. ActivityType $9.;
  format Date date.;
  datalines;
B123D 11/01/2020 APP_New
B123D 11/01/2020 APP_New
A932F 11/01/2020 APP_Edit
C456E 11/01/2020 APP_New
B123D 11/02/2020 APP_Edit
A932F 11/02/2020 APP_New
C456E 11/02/2020 APP_Edit
C456E 11/02/2020 APP_New
;
run;

proc sort data=input;
  by date id  ActivityType;
run;

data output;
  set input;
  by date id ;
  retain Total_App_New Total_App_Edit;

  if first.id then 
    do; 
      Total_App_New=0;
      Total_App_Edit=0;
    end;

  if ActivityType = 'APP_New' then Total_App_New+1;
  else if ActivityType = 'APP_Edit' then Total_App_Edit+1;

  if last.id then output;
run;

count.png

 

novinosrin
Tourmaline | Level 20

Hi @BlayLay  You could resort to lazy way of doing it by utilizing Proc Freq SPARSE option and a simple transpose. Increasingly I am getting used to these methods-

 

data input;
  input ID $5. Date :mmddyy10. ActivityType $9.;
  format Date date9.;
  datalines;
B123D 11/01/2020 APP_New
B123D 11/01/2020 APP_New
A932F 11/01/2020 APP_Edit
C456E 11/01/2020 APP_New
B123D 11/02/2020 APP_Edit
A932F 11/02/2020 APP_New
C456E 11/02/2020 APP_Edit
C456E 11/02/2020 APP_New
;
run;

proc freq data=input;
 tables date*id*ActivityType/sparse noprint out=temp(drop=percent);
run;

proc transpose data=temp out=want(drop=_:) prefix=Total_;
 by date id;
 var count;
 id ActivityType;
run;
GertNissen
Barite | Level 11

Great @novinosrin 👍

 

Good use of sparse - Was not aware of this one - very useful - thanks 

PhilC
Rhodochrosite | Level 12

Ditto


@GertNissen wrote:

Great @novinosrin 👍

Good use of sparse - Was not aware of this one - very useful - thanks 


 

novinosrin
Tourmaline | Level 20

Thank you @PhilC  , @GertNissen  for the note.  To be totally honest, I was taught these tricks by @PGStats  a while ago. Of course I tend to take notes of such geniuses , and this being yet another that PG advised me to quickly grasp three major equivalent options i.e-

 

Proc FREQ- SPARSE

Proc Summary/Means - COMPLETETYPES

Proc REPORT- COMPLETEROWS

 

Sir @PGStats  Whether you remember or not, Thank you sharing this that now these learning are nicely shared across the our platform. Cheers!

BlayLay
Obsidian | Level 7
Thank you! This worked perfectly
Ksharp
Super User
data input;
  input ID $5. Date :mmddyy10. ActivityType $9.;
  format Date date9.;
  datalines;
B123D 11/01/2020 APP_New
B123D 11/01/2020 APP_New
A932F 11/01/2020 APP_Edit
C456E 11/01/2020 APP_New
B123D 11/02/2020 APP_Edit
A932F 11/02/2020 APP_New
C456E 11/02/2020 APP_Edit
C456E 11/02/2020 APP_New
;
run;
proc sql noprint;
select distinct cats("sum(ActivityType='",ActivityType,"') as total_",ActivityType)
 into : list separated by ','
  from input;
create table want as
select date,id,&list 
 from input
  group by date,id;
quit;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 7 replies
  • 1831 views
  • 2 likes
  • 5 in conversation