DATA Step, Macro, Functions and more

Proc SQL Unique Counts and Categories

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

Proc SQL Unique Counts and Categories

Hi everyone,

 

I'd like to get a count of all the unique cases I have in a dataset. The code I am using is only giving me the counts but not the categories associated with it. Here is my code:

 

proc sql;
 select count(unique(ID)) as u_ID 'Number of Unique Ids'
 from MyData
 group by Age
;quit;

Here are my results:

Number of Number of Unique Ids
19
99
58
110
454
18
52
2
74
22
4
8
9
12
88
4
9
12

 

This is what I would like:

 

AgeNumber of Number of Unique Ids
019
199
258
3110
4454
518
652
72
874
922
104
118
129
1312
1488
154
169
1712

 

Any advice would be greatly appreciated! Thanks! 


Accepted Solutions
Solution
‎11-15-2016 12:23 PM
PROC Star
Posts: 547

Re: Proc SQL Unique Counts and Categories

Or

 

proc sql;
 select Age, count(unique(ID)) as u_ID 'Number of Unique Ids'
 from MyData
 group by Age
;quit;

View solution in original post


All Replies
PROC Star
Posts: 547

Re: Proc SQL Unique Counts and Categories

Use PROC FREQ? Smiley Happy

Solution
‎11-15-2016 12:23 PM
PROC Star
Posts: 547

Re: Proc SQL Unique Counts and Categories

Or

 

proc sql;
 select Age, count(unique(ID)) as u_ID 'Number of Unique Ids'
 from MyData
 group by Age
;quit;
Frequent Contributor
Posts: 103

Re: Proc SQL Unique Counts and Categories

Hi,

as #draycut proposed you should consider using proc freq.

ods select none;
ods output oneWayFreqs=want(keep=age frequency);
proc freq data=sashelp.class;
table age;
;
run;
ods select all;

Cheers

________________________

- That still only counts as one -

Contributor
Posts: 50

Re: Proc SQL Unique Counts and Categories

Thanks for the response, proc freq would not work because I wanted the unique cases. Proc freq could give me the frequency for all cases.
Super User
Posts: 9,662

Re: Proc SQL Unique Counts and Categories

Actually proc freq can do that. you need NLEVELS option;

proc freq ......    nlevels ;
........

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 258 views
  • 0 likes
  • 4 in conversation