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

I have the following data.

 

data test;
input period date9. case $;
format period date9.;
datalines;
11DEC2018 ABC1
12DEC2018 ABC2
13DEC2018 ABC1
13DEC2018 ABC2
01JAN2019 ABC1
02JAN2019 ABC2

;

Run;

 

May I know how to get the desired output - cases by month? Thanks.

 

Desired Output:

 

Period            Cases

DEC2018        4

JAN2019         2

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

One way

 

data test;
input period date9. case $;
format period date9.;
datalines;
11DEC2018 ABC1
12DEC2018 ABC2
13DEC2018 ABC1
13DEC2018 ABC2
01JAN2019 ABC1
02JAN2019 ABC2
;
Run;

proc sql;
   create table want as
   select put(period, monyy7.) as period,
          count(period) as Cases
   from test
   group by calculated period;
quit;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

One way

 

data test;
input period date9. case $;
format period date9.;
datalines;
11DEC2018 ABC1
12DEC2018 ABC2
13DEC2018 ABC1
13DEC2018 ABC2
01JAN2019 ABC1
02JAN2019 ABC2
;
Run;

proc sql;
   create table want as
   select put(period, monyy7.) as period,
          count(period) as Cases
   from test
   group by calculated period;
quit;
andreas_lds
Jade | Level 19

Another approach:

proc summary data=work.test nway;
   class period;
   format period monyy7.;
   output out=work.want(drop=_type_ rename=(_freq_=Cases));
run;
novinosrin
Tourmaline | Level 20

Good morning, Sure I was sleeping and too late to the party

 

data test;
input period date9. case $;
format period date9.;
datalines;
11DEC2018 ABC1
12DEC2018 ABC2
13DEC2018 ABC1
13DEC2018 ABC2
01JAN2019 ABC1
02JAN2019 ABC2
;
Run;

data _null_;
if _n_=1 then do;
   dcl hash H (ordered: "A") ;
   h.definekey  ("Period") ;
   h.definedata ("Period","cases") ;
   h.definedone () ;
end;
set test(rename=period=_period) end=lr;
Period=put(_period,monyy7.);
retain _p;
if period ne _p then do;cases=1;_p=period;end;
else cases+1;
h.replace();
if lr then h.output(dataset:'want');
run;

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!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 726 views
  • 0 likes
  • 4 in conversation