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

data have;

input ID1 $ ID2 $;

cards;

A O

B A

C D
D C

E P

F .
G F
H B
I K
I L
I M
;

run;

I my dataset I have 9 distinct ID1 and and 5 distinct ID2. I want to calculate the total number of distinct id in column ID1 or ID2 . The count should be 14 .

I am wondering if this can be done in proc sql instead of proc freq? If this can be done in proc sql what would the code be?

 

thanks

 

KC

1 ACCEPTED SOLUTION

Accepted Solutions
gamotte
Rhodochrosite | Level 12

OK, the data step you gave in your question has some formatting problems so your

goal was unclear.

 

Try :

proc sql;
SELECT count(DISTINCT ID)
FROM (
    SELECT ID1 AS ID FROM have
	UNION
	SELECT ID2 AS ID FROM have
);
quit;

View solution in original post

5 REPLIES 5
gamotte
Rhodochrosite | Level 12

Hello,

 

Something like this ?

 

proc sql;
SELECT sum(count(DISTINCT ID1), count(DISTINCT ID2)) 
FROM have;
quit;
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

No this is not correct because ID2 has some ID1 which are in the column ID1. I only want the total of IDs which are unique to ID1 ad ID2, which would be (A,B,C,D,E,F,G,J ,I ,K,L,M,P,O= count=14).

 

The sum will sum the unique ID1 and Unique ID2

gamotte
Rhodochrosite | Level 12

OK, the data step you gave in your question has some formatting problems so your

goal was unclear.

 

Try :

proc sql;
SELECT count(DISTINCT ID)
FROM (
    SELECT ID1 AS ID FROM have
	UNION
	SELECT ID2 AS ID FROM have
);
quit;
Kc2
Quartz | Level 8 Kc2
Quartz | Level 8

Thank you it worked

andreas_lds
Jade | Level 19

@Kc2 if the code suggested by @gamotte  does not solve the problem, please re-post the data step, this time using the running-man-icon to preserve formatting.

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
  • 5 replies
  • 2699 views
  • 0 likes
  • 3 in conversation