Hi all!
I know how to count observations by group, for example:
data temp;
set dataset;
by keyvar;
length per 8.;
if first.keyvar then per=0;
per+1;
run;
This code returns output that looks like this:
keyvar per(count)
a | 1 |
b | 1 |
b | 2 |
b | 3 |
c | 1 |
c | 2 |
c | 3 |
c | 4 |
Instead I want the output to look like this:
keyvar per(count)
a | 1 |
b | 3 |
b | 3 |
b | 3 |
c | 4 |
c | 4 |
c | 4 |
c | 4 |
Does anyone have any ideas for me?
Thanks! Hope to hear from people!
Make a dummy variable.
data have;
input keyvar $ ;
dummy=1;
cards;
a 1
b 1
b 2
b 3
c 1
c 2
c 3
c 4
;
proc sql;
create table want(drop=dummy) as
select *,count(*) as count
from have
group by keyvar;
quit;
PROC FREQ will count the observations by group, then you can merge the results with the original data. Or PROC SQL can do this as well.
proc freq data=have;
tables keyvar/out=counts;
run;
data want;
merge have counts;
by keyvar;
run;
thanks for this! do you have an example with proc sql?
Hello @mallorybane
data have;
input keyvar $ ;
cards;
a 1
b 1
b 2
b 3
c 1
c 2
c 3
c 4
;
proc sql;
create table want as
select a.*,count
from
have a, (select keyvar, count(keyvar)as count from have group by keyvar) b
where a.keyvar=b.keyvar
order by a.keyvar;
quit;
And if you are using SAS 9.4
data _null_ ;
if _n_=1 then do;
dcl hash H (ordered: "A",multidata:'y') ;
h.definekey ("keyvar") ;
h.definedata ("keyvar", "count") ;
h.definedone () ;
end;
do count=1 by 1 until(last.keyvar);
set have end=lr;
by keyvar;
rc=h.add();
end;
rc=h.replace();
if lr then h.output(dataset:'want') ;
run;
Simple double DOW, prolly the fastest
data want ;
if 0 then set have;
do count=1 by 1 until(last.keyvar);
set have ;
by keyvar;
end;
do until(last.keyvar);
set have;
by keyvar;
output;
end;
run;
Using FIRST and LAST but interleave
data want;
set have(in=a) have(in=b);
by keyvar;
if first.keyvar then count=0;
if a then count+1;
if b then output;
run;
Very nice @novinosrin !
Thank you Sir @PGStats . Hopefully as good as you one day however a farfetched dream as of now. Merci beacoup!
Make a dummy variable.
data have;
input keyvar $ ;
dummy=1;
cards;
a 1
b 1
b 2
b 3
c 1
c 2
c 3
c 4
;
proc sql;
create table want(drop=dummy) as
select *,count(*) as count
from have
group by keyvar;
quit;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.