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
- /
- Base SAS Programming
- /
- Descending order frequencies in proc freq (order=f...

Topic Options

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

04-06-2017 01:37 AM

```
proc freq data=have order=freq ;
tables var1/list missing out=var1_freq;
run;
```

The proc freq above outputs by descending order of frequency of var1.

I need the opposite, which is by ascending order.

And the output only gives the frequency and percentage. How can I get the cumulative freq and cumulative percentage as well? These two should vary depending on how you order the var1.

Accepted Solutions

Solution

08-31-2017
02:29 PM

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

Posted in reply to afiqcjohari

04-06-2017 11:06 AM - last edited on 08-31-2017 02:28 PM by Kathryn_SAS

*Editor's Note: The documentation for PROC FREQ defines the ORDER=FREQ option as the descending frequency count. Unfortunately, there is not an option to make this ascending. Other procedures, such as PROC REPORT, define ORDER=FREQ as the ascending frequency count. However, given that the cumulative statistics are also desired, the PROC FREQ and DATA Step solution provided by @ballardw is best for this case.*

Example input data and desired output will go a long way toward reducing suggestions that do not answer non-specified requirements. Your first post is change the order. And suggestions follow. After that you request that the cumulative totals be reordered, not in the initial post.

And when you start getting into non-standard behaviors then you get to do a little more work. Not much. Please see:

proc freq data=sashelp.class noprint; tables age / out=agefreq; run; proc sort data=agefreq; by count; run; data want; set agefreq;

cumcount + count; cumpercent + percent; run;

All Replies

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

Posted in reply to afiqcjohari

04-06-2017 02:37 AM

to get the cumulative frequency and cumulative percent please mention OUTCUM in the tables

proc freq data=have order=freq ;

tables var1/OUTCUM list missing out=var1_freq;

run;

to sort the data by ascending frequency, use proc sort

proc sort data=var1_freq;

by count;

run;

proc freq data=have order=freq ;

tables var1/OUTCUM list missing out=var1_freq;

run;

to sort the data by ascending frequency, use proc sort

proc sort data=var1_freq;

by count;

run;

Thanks,

Jag

Jag

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

Posted in reply to Jagadishkatam

04-06-2017 03:11 AM

Sorting after the proc freq won't work because the cumulative frequency is not 'sorted' accordingly.

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

Posted in reply to afiqcjohari

04-06-2017 03:25 AM

you could also mention the cumulative frequency variable in the proc sort depending on how you want to sort the data after proc freq

Thanks,

Jag

Jag

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

Posted in reply to Jagadishkatam

04-06-2017 06:02 AM

The cumulative frequency is calculated based on how the sorting is done. Sorting after won't do because the cumulative frequency no longer reflects the table properly.

Solution

08-31-2017
02:29 PM

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

Posted in reply to afiqcjohari

04-06-2017 11:06 AM - last edited on 08-31-2017 02:28 PM by Kathryn_SAS

*Editor's Note: The documentation for PROC FREQ defines the ORDER=FREQ option as the descending frequency count. Unfortunately, there is not an option to make this ascending. Other procedures, such as PROC REPORT, define ORDER=FREQ as the ascending frequency count. However, given that the cumulative statistics are also desired, the PROC FREQ and DATA Step solution provided by @ballardw is best for this case.*

Example input data and desired output will go a long way toward reducing suggestions that do not answer non-specified requirements. Your first post is change the order. And suggestions follow. After that you request that the cumulative totals be reordered, not in the initial post.

And when you start getting into non-standard behaviors then you get to do a little more work. Not much. Please see:

proc freq data=sashelp.class noprint; tables age / out=agefreq; run; proc sort data=agefreq; by count; run; data want; set agefreq;

cumcount + count; cumpercent + percent; run;

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

Posted in reply to ballardw

04-06-2017 09:30 PM

Agree on providing the desired output. But I explicitly mentioned that the cumulative totals should be reordered according to the sort. So it was in the initial post.

Otherwise I'm quite surprised that PROC FREQ can't have an option for that?

Furthermore, it's more common to perform cumulative frequency in ascending order as oppose to descending order. So I would expect that PROC Freq has a much simpler code for this.

Nothing like this for example?

proc freq data=sashelp.class order= DESCENDING freq ;

tables age/OUTCUM list missing out=agefreq2;

run;

Otherwise I'm quite surprised that PROC FREQ can't have an option for that?

Furthermore, it's more common to perform cumulative frequency in ascending order as oppose to descending order. So I would expect that PROC Freq has a much simpler code for this.

Nothing like this for example?

proc freq data=sashelp.class order= DESCENDING freq ;

tables age/OUTCUM list missing out=agefreq2;

run;