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

Hello. I am trying to get a table of cumulative Counts grouped by

two variables, a Category UpNum and a Date (M_Y). Do you have any

code ideas? Thanks!!

 

Input Table                                                      Desired Output           

UpNum     M_Y                             UpNum         M_Y       M_Ycount    cum_Count

XXA        1/2018                              XXA         1/2018           1                1

XXA         4/2018                             XXA          2/2018          0                1

XXB         2/2018                             XXA          3/2018          1                2

XXB         2/2018                             XXA          4/2018          1                3                  

XXA        3/2018                               .....              ......            (list of all XXA, M_Y combinations)

XXB         4/2018                             XXB           1/2018          0               0

XXA         6/2018                             XXB           2/2018          2               2

.....           ......                                  XXB           3/2018          0               2

( XXC, XXD, all M_Y dates, etc)     XXB           4/2018          1               3

                                                         .......             .......         (list of all XXB, M_Y combinations)

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

Using nov's code .

 

                                  
data have;
input UpNum $    M_Y    $  ; 
cards;
XXA        1/2018                             
XXA         4/2018                             
XXB         2/2018                            
XXB         2/2018                                   
XXA        3/2018                        
XXB         4/2018                           
XXA         6/2018 
;
proc freq data=have noprint;
table UpNum*M_Y/out=temp(drop= percent) sparse ;
run;
data want;
 set temp;
 by upnum ;
 if first.upnum then want=0;
  want+count;
run;

View solution in original post

3 REPLIES 3
novinosrin
Tourmaline | Level 20

@crawfe   The sparse option in proc freq will give you the combination UpNum*M_Y

 

You can use the output of proc freq for as input in one datastep pass. The datastep is rather straight forward, the only thought process here is cartesian either using SQL or Sparse etc to get the combination

 


                                   
data have;
input UpNum $    M_Y    $  ; 
cards;
XXA        1/2018                             
XXA         4/2018                             
XXB         2/2018                            
XXB         2/2018                                   
XXA        3/2018                        
XXB         4/2018                           
XXA         6/2018 
;
proc freq data=have noprint;
table UpNum*M_Y/out=temp(drop=percent) sparse ;
run;

Now use the temp dataset, merge back with have with IN=dataset options for your cumulative count

Ksharp
Super User

Using nov's code .

 

                                  
data have;
input UpNum $    M_Y    $  ; 
cards;
XXA        1/2018                             
XXA         4/2018                             
XXB         2/2018                            
XXB         2/2018                                   
XXA        3/2018                        
XXB         4/2018                           
XXA         6/2018 
;
proc freq data=have noprint;
table UpNum*M_Y/out=temp(drop= percent) sparse ;
run;
data want;
 set temp;
 by upnum ;
 if first.upnum then want=0;
  want+count;
run;
crawfe
Quartz | Level 8

That worked well. Thanks!

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1172 views
  • 0 likes
  • 3 in conversation