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

I know if i want to find how many distinct a, I can use count (distinct a)

Now I need find how many different combination of a and b.

What I should do?

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

Another option:

 

  select count(distinct catx('.',sex,age) ) as n_sex_by_age from have ;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

3 REPLIES 3
jimbarbour
Meteorite | Level 14

If you create two temporary tables consisting of one with just A and the other with Just B, then join them to get the Cartesian product (no where clause or INNER JOIN) and then do a SELECT COUNT DISTINCT on the Cartesian product, I think that should give you the number you want.

 

Some thing like this but check my syntax:

PROC SQL;

  CREATE TABLE A_TAB as SELECT DISTINCT A FROM Source_Table;

  CREATE TABLE B_TAB as SELECT DISTINCT B FROM Source_Table;

  CREATE TABLE A_B_TAB as SELECT * FROM A_TAB, B_TAB;

  SELECT COUNT(DISTINCT *) FROM A_B_TAB;

QUIT;

Jim

CurtisMackWSIPP
Lapis Lazuli | Level 10
 select count(*) from (select distinct a, b from HAVE) ;
mkeintz
PROC Star

Another option:

 

  select count(distinct catx('.',sex,age) ) as n_sex_by_age from have ;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1311 views
  • 1 like
  • 4 in conversation