BookmarkSubscribeRSS Feed
AshleyM
Fluorite | Level 6

Hi there,

I'm trying to run a proc frequency on a variable (wsb6) where the value range is 1-9, but want to include zero values in the frequency chart. For instance, if the value 7 was not reported for wsb6, I'd like to show zero in the frequency table for that value. I'm not sure what to do. 

libname jess "Desktop";

run;

proc freq data=jess.exampledata;

tables wsb6;

run;

3 REPLIES 3
ballardw
Super User

Proc freq is probably not what you want as there isn't any way to tell it to show something that is not in the actual data.

Procs tabulate and report have an option, Preloadfmt, that can force all values of a categorical variable to be displayed even if not data exists but requires creating the format beforehand.

proc format library=work;

value wsb

1 = '1' 2='2' 3='3' 4='4' 5='5' 6='6' 7='7' 8='8' 9='9';

run;

proc tabulate data=jess.exampledata;

     class wsb6 / preloadfmt order=data;

     format wsb6 wsb.;

     table wsb6, n pctn / printmiss misstext='0';

run;

Should do something close to what you want.

UrvishShah
Fluorite | Level 6

Hi,

It is also possible by using the following code...However, by default Proc Freq produce the freq count of existing values...

proc freq data = test noprint;

   tables var / out = test1(drop = percent);

run;

data test2;

  do i = 1 to 9;

     output;

  end;

run;

proc sql;

   create table final as

   select case

              when a.var ^= i then i

              else a.var

          end as var,

          case

              when count = . then 0

              else count = count

          end as count

   from test1 as a right join

        test2 as b

   on a.var = b.i;

quit;

You need to convert your variable to Numeric if it is charcter...and then run the above code...

-Urvish

Ksharp
Super User

or merge it to make it complete.

proc freq data = test noprint;
   tables wsb6 / out = test1(drop = percent);
run;

 

data test2;
retain count 0;
  do wsb6 = 1 to 9;
     output;
  end;
run;
data want;
 merge test2 test1 ;
 by wsb6;
run;


Ksharp

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!

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.

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
  • 3 replies
  • 2423 views
  • 1 like
  • 4 in conversation