BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hein68
Obsidian | Level 7

When using proc freq, what code is needed to include levels with frequencies equaling 0 in your output?

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
So you have a value but that value doesn't appear anywhere in your data and you want SAS to put a 0 for that unknown value?

You can look into PRELOADFMT or CLASSDATA within PROC TABULATE/MEANS which deal with this better but a quick trick for PROC FREQ is to use the SPARSE option. BUT...it only works if that value is listed at least once within other categories.
https://blogs.sas.com/content/sastraining/2013/10/18/sas-authors-tip-using-the-sparse-option-with-pr...

https://www.pharmasug.org/proceedings/2012/CC/PharmaSUG-2012-CC26.pdf

View solution in original post

4 REPLIES 4
Reeza
Super User
So you have a value but that value doesn't appear anywhere in your data and you want SAS to put a 0 for that unknown value?

You can look into PRELOADFMT or CLASSDATA within PROC TABULATE/MEANS which deal with this better but a quick trick for PROC FREQ is to use the SPARSE option. BUT...it only works if that value is listed at least once within other categories.
https://blogs.sas.com/content/sastraining/2013/10/18/sas-authors-tip-using-the-sparse-option-with-pr...

https://www.pharmasug.org/proceedings/2012/CC/PharmaSUG-2012-CC26.pdf
ballardw
Super User

@hein68 wrote:

When using proc freq, what code is needed to include levels with frequencies equaling 0 in your output?

Thanks!


I think you need to be a bit more detailed in your question such as providing some example data and exactly what "level" that has a frequency of 0 that should be counted.

 

Are you talking about a combination of variables or a single variable?

FreelanceReinh
Jade | Level 19

Hi @hein68,

 

Another trick is to use the WEIGHT statement with the ZEROS option:

/* Create test data for demonstration */

data have;
set sashelp.class;
run;

/* Introduce age levels with frequency zero */

data want / view=want;
do until(last);
  set have end=last;
  _w=1;
  output;
end;
do age=5, 18, 99;
  _w=0;
  output;
end;
run;

/* Create frequency table including the new levels */

proc freq data=want;
weight _w / zeros;
tables age;
run;

Output:

                                Cumulative    Cumulative
Age    Frequency     Percent     Frequency      Percent
--------------------------------------------------------
  5           0        0.00             0         0.00
 11           2       10.53             2        10.53
 12           5       26.32             7        36.84
 13           3       15.79            10        52.63
 14           4       21.05            14        73.68
 15           4       21.05            18        94.74
 16           1        5.26            19       100.00
 18           0        0.00            19       100.00
 99           0        0.00            19       100.00

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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