Help using Base SAS procedures

counting total number of distinct observation

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

counting total number of distinct observation

Hi everyone,

I have a data set that contain a variable similar to this example,

 

number

56

56

56

58

58

60

61

61

61

61

61

...

 

I would like to know the total count of distinct number. In this example is 4 (56, 58, 60, and 61). 

How can i do it in SAS? I think i can do

proc sort data= mydata out=dataout noduplications;

... 

then use proc freq or proc means

but is that a clever way?

 

thanks 

 


Accepted Solutions
Solution
‎01-25-2018 04:50 AM
Trusted Advisor
Posts: 1,837

Re: counting total number of distinct observation

[ Edited ]
Posted in reply to babymonster

There are more than one way to get the amount of distinct values of a variable:

 

1) Using proc sort with nodupkey - just check number of output observations in log or check output dataset.

 

2) Use proc sql - select distinct variable and end with:

  

quit;
%put &sqlobs;

  then check the log;

 

3) Assuming your data is sorted by the variable then do:

  

data _null_;
   set have end=eof;
        retain distinct_count 0;
    by var;
        if eof then put   distinct_count =;
        if first.var then  distinct_count  +1;
run;

  and check the log;

View solution in original post


All Replies
Solution
‎01-25-2018 04:50 AM
Trusted Advisor
Posts: 1,837

Re: counting total number of distinct observation

[ Edited ]
Posted in reply to babymonster

There are more than one way to get the amount of distinct values of a variable:

 

1) Using proc sort with nodupkey - just check number of output observations in log or check output dataset.

 

2) Use proc sql - select distinct variable and end with:

  

quit;
%put &sqlobs;

  then check the log;

 

3) Assuming your data is sorted by the variable then do:

  

data _null_;
   set have end=eof;
        retain distinct_count 0;
    by var;
        if eof then put   distinct_count =;
        if first.var then  distinct_count  +1;
run;

  and check the log;

New Contributor
Posts: 2

Re: counting total number of distinct observation

Thanks Shmuel
Super User
Super User
Posts: 9,599

Re: counting total number of distinct observation

Posted in reply to babymonster

You want to do a frequency count, therefore use proc freq(uency) - this is logical and why they called the procedure that name

Super User
Posts: 13,583

Re: counting total number of distinct observation

Posted in reply to babymonster

And an example using the Proc Freq nlevels option:

ods select nlevels;
proc freq data=sashelp.class nlevels;
   tables sex;
run;

the ODS Select nlevels; instructs SAS to only show the levels information which would be the number of unique values and missing values if any.

 

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 266 views
  • 1 like
  • 4 in conversation