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.
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;
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.
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;
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.