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.

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 1208 views
  • 2 likes
  • 3 in conversation