turn on suggestions

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

Showing results for

Find a Community

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

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

01-25-2018 03:58 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to babymonster

01-25-2018 04:11 AM - edited 01-25-2018 04:50 AM

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;

All Replies

Solution

01-25-2018
04:50 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to babymonster

01-25-2018 04:11 AM - edited 01-25-2018 04:50 AM

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
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Shmuel

01-25-2018 04:45 AM

Thanks Shmuel

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to babymonster

01-25-2018 05:29 AM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to babymonster

01-25-2018 11:35 AM

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.