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

I would like to count numbers of distinct cases that meet different criteria in my large dataset. The example data looks like below:

obsidvar
1a
1b
1b
2b
2b
2c
2c
3a
3a
4c

I need to see how many distinct obsids that contain "a", "b" or "c" in var.

I could do for different criteria one after another like this:

proc sql; select count(distinct obsid) as N_a from project.dswi_nodebride where var="a"; run;

proc sql; select count(distinct obsid) as N_b from project.dswi_nodebride where var="b"; run;

proc sql; select count(distinct obsid) as N_c from project.dswi_nodebride where var="c"; run;

I have many criteria, so is there a simpler way to do for all different but similar criteria?

Thanks a lot!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

SQL is ideal for this:

data have;

input obsid var$;

datalines;

1 a

1 b

1 b

2 b

2 b

2 c

2 c

3 a

3 a

4 c

;

proc sql;

create table want as

select var, count(distinct obsid) as n

from have

group by var;

select * from want;

quit;

PG

PG

View solution in original post

4 REPLIES 4
stat_sas
Ammonite | Level 13

proc sql;

select var,count(distinct obsid) as N from have

group by var;

quit;

PGStats
Opal | Level 21

SQL is ideal for this:

data have;

input obsid var$;

datalines;

1 a

1 b

1 b

2 b

2 b

2 c

2 c

3 a

3 a

4 c

;

proc sql;

create table want as

select var, count(distinct obsid) as n

from have

group by var;

select * from want;

quit;

PG

PG
data_null__
Jade | Level 19

This is somewhat like counting subjects with adverse events.  This counts events and obsids for each level of var.

data ae;
   infile cards expandtabs;
  
input obsid var :$1.;
  
cards;
1  a
1  b
1  b
2  b
2  b
2  c
2  c
3  a
3  a
4  c
;;;;
   run;
proc print;
  
run;
proc summary data=ae nway;
  
class var obsid;
   output out=events(drop=_type_ rename=(_freq_=events));
   run;
proc print;
  
run;
proc summary data=events nway;
  
class var;
   output out=obsids(drop=_type_ rename=(_freq_=obsids)) sum(events)=;
   run;
proc print;
  
run;

10-31-2014 12-12-12 PM.png
lizzy28
Quartz | Level 8

Thanks so much to you all!

Lizi

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 1813 views
  • 6 likes
  • 4 in conversation