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.

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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