BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

Id
a
b
c
a
b
a
b
c
b

How to get counts of id and then sum the total number of id's and display the below format in a table?

a b c total
3 4 2 9

5 REPLIES 5
novinosrin
Tourmaline | Level 20

Can it be done in two steps?

 

PeterClemmensen
Tourmaline | Level 20

One way

 

data have;
input Id $;
datalines;
a
b
c
a
b
a
b
c
b
;

proc freq data=have;
   tables Id / out=temp;
run;

proc transpose data=temp out=tempwide;
    id Id;
    var count;
run;

data want;
   set tempwide(keep=_NUMERIC_);
   total=sum(of _NUMERIC_);
run;
novinosrin
Tourmaline | Level 20

data have;
input Id $;
cards;
a
b
c
a
b
a
b
c
b
;

/*Need this step for horizontal variables names*/
proc sql noprint;
select distinct id,count(distinct id)  into :vars separated by ' ',:c
from have;
quit;
/*straight forward look up and count*/
data want;
set have end=lr;
array names(&c) &vars;
array temp(&c) $ _temporary_ ;
if _n_=1 then do _n_=1 to dim(temp);
temp(_n_)=vname(names(_n_));
end;
names(whichc(id,of temp(*)))+1;
if lr;
total=sum(of names(*));
output;
drop id;
run;
ballardw
Super User

@SASPhile wrote:

Id
a
b
c
a
b
a
b
c
b

How to get counts of id and then sum the total number of id's and display the below format in a table?

a b c total
3 4 2 9


How about:

data have;
input Id $;
datalines;
a
b
c
a
b
a
b
c
b
;
run;

proc tabulate data=have;
   class id;
   table n='',
         id='' All='Total'
         /row=float
   ;
quit;
novinosrin
Tourmaline | Level 20

"Speak of the SAS genie", I was just mentioning to a mate that you will come up with a proc tabulate and you did. 🙂 Kudos to me for predicting haha and you lol

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 5 replies
  • 1177 views
  • 6 likes
  • 4 in conversation