Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- SAS Procedures
- /
- counting total number of distinct observation

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 01-25-2018 03:58 AM
(5742 views)

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

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

4 REPLIES 4

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks Shmuel

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

Are you ready for the spotlight? We're accepting content ideas for **SAS Innovate 2025** to be held May 6-9 in Orlando, FL. The call is **open **until September 16. Read more here about **why** you should contribute and **what is in it** for you!

What is Bayesian Analysis?

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.