BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mallorybane
Calcite | Level 5

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)

a1
b1
b2
b3
c1
c2
c3
c4

 

Instead I want the output to look like this:

 

keyvar     per(count)

a1
b3
b3
b3
c4
c4
c4
c4

 

Does anyone have any ideas for me?

 

Thanks! Hope to hear from people!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
mallorybane
Calcite | Level 5

thanks for this! do you have an example with proc sql?

novinosrin
Tourmaline | Level 20

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;
novinosrin
Tourmaline | Level 20

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;
novinosrin
Tourmaline | Level 20

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;
novinosrin
Tourmaline | Level 20

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;

 

 

novinosrin
Tourmaline | Level 20

Thank you Sir @PGStats . Hopefully as good as you one day however a farfetched dream as of now. Merci beacoup!

Ksharp
Super User

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;

SAS Innovate 2025: Register Now

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!

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
  • 9 replies
  • 1153 views
  • 2 likes
  • 5 in conversation