Help using Base SAS procedures

Does proc freq nlevels count unique observations?

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

Does proc freq nlevels count unique observations?

proc freq data=x nlevels;

table patient_id;

run;

I am trying to count the # of unique patients in this data.

Does the resulting output show the number of unique patient ids? or just the total amount of observations with non-missing ids.

Thank you, I appreciate the help.


Accepted Solutions
Solution
‎07-22-2014 11:22 AM
Super User
Posts: 10,500

Re: Does proc freq nlevels count unique observations?

If you need count of unique IDS:

proc sql:

     select count(*)

     from (select DISTINCT patient_id from x);

quit;

If you need list of unique ids:

proc sql;

     select distinct Patient_id

     from x;

quit;

View solution in original post


All Replies
Super User
Posts: 17,826

Re: Does proc freq nlevels count unique observations?

Trusted Advisor
Posts: 1,204

Re: Does proc freq nlevels count unique observations?

It will provide total number of observations for each level of patient_id. So if each patient_id has one frequency then we can conclude that patient_id is unique.

Super User
Posts: 17,826

Re: Does proc freq nlevels count unique observations?

Not quite, NLEVELS does produce the number of distinct observations in the number of levels table.

Be careful of how it treats missing and how you want missing values treated though.

proc freq data=sashelp.class nlevels;

table age;

run;

Solution
‎07-22-2014 11:22 AM
Super User
Posts: 10,500

Re: Does proc freq nlevels count unique observations?

If you need count of unique IDS:

proc sql:

     select count(*)

     from (select DISTINCT patient_id from x);

quit;

If you need list of unique ids:

proc sql;

     select distinct Patient_id

     from x;

quit;

Regular Learner
Posts: 1

Re: Does proc freq nlevels count unique observations?

To answer your question OP, the nlevels option will count distinct values, including missing values. The following code can help you break down multiple variables into unique values missing and non-missings much easier than performing the same task with a proc sql:

 

ods output nlevels=LEVELS;
proc freq data=dataset nlevels;
tables var1 var2 var3 var4 / noprint ;
run;
proc print data=LEVELS;
run;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 3094 views
  • 6 likes
  • 5 in conversation