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

Hi I am thinking is it possible to count distinct combination of two variables, for example:

 

I know proc sql;

count(distinct (variable))

but it only count base on one criteria...

 

person_IDcondition_Id
11
12
11
22
31
31
42
41
42
51
52
61

 

this should give me a count of 9 (person 1 was count twice although it has 3 entries, person 3 count only 1 although it has 2 entries).

 

All ideas are welcome!

 

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Distinct can also be used at the row level:

 

data test;
input person_ID condition_Id ;
datalines;
1 1 
1 2 
1 1 
2 2 
3 1 
3 1 
4 2 
4 1 
4 2 
5 1 
5 2 
6 1 
;

proc sql;
select count(*) as n
from
    (select distinct person_id, condition_id
     from test);
quit;
PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

Distinct can also be used at the row level:

 

data test;
input person_ID condition_Id ;
datalines;
1 1 
1 2 
1 1 
2 2 
3 1 
3 1 
4 2 
4 1 
4 2 
5 1 
5 2 
6 1 
;

proc sql;
select count(*) as n
from
    (select distinct person_id, condition_id
     from test);
quit;
PG
LisaYIN9309
Obsidian | Level 7
This is great! Than you!
Astounding
PROC Star

The SQL gurus may shoot me over this, but as long as both variables are numeric you could use:

 

proc sql;

select count (distinct(person_ID + condition_ID/10)) from have;

quit;

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 3 replies
  • 27161 views
  • 3 likes
  • 3 in conversation