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
Can it be done in two steps?
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;
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;
@SASPhile wrote:
Id
a
b
c
a
b
a
b
c
bHow 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;
"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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.