BookmarkSubscribeRSS Feed
radha009
Quartz | Level 8

i want to create a table similar to below, for an id multiple and unique summary descriptions and the total count. Is it possible?

 

 

 

idsummarysummary cnttotal_ct
123Oranges12
 Grapes1 
345blueberry13
 Banana2 
 Apple1 
3 REPLIES 3
Kurt_Bremser
Super User

To exactly create your output, you need proc report with an intermediate step:

data have;
input id $ summary :$10. summary_cnt;
cards;
123 Oranges 1
123 Grapes 1
345 blueberry 1
345 Banana 2
345 Apple 1
;
run;

data want;
if _n_ = 0 then set have;
total_ct = 0;
do until (last.id);
  set have;
  by id;
  total_ct + 1;
end;
do until (last.id);
  set have;
  by id;
  output;
  total_ct = .;
end;
run;

options missing = ' ';

proc report data=want;
column id summary summary_cnt total_ct;
define id / group;
define summary / display;
define summary_cnt / display;
define total_ct / display;
run;

Result:

                      summary_c           
id        summary            nt   total_ct
123       Oranges             1          2
          Grapes              1           
345       blueberry           1          3
          Banana              2           
          Apple               1           

 

Edit: added options missing.

jonna28pr0
Fluorite | Level 6

Sir,

 

Could you please make me understand what happens if we don't use "if _n_ = 0 then set have;" statement? Thanks.

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 3 replies
  • 671 views
  • 2 likes
  • 3 in conversation