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

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 711 views
  • 1 like
  • 4 in conversation