Help using Base SAS procedures

Proc freq and zeros

Reply
Contributor
Posts: 48

Proc freq and zeros

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;

Super User
Posts: 10,500

Re: Proc freq and zeros

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.

Regular Contributor
Posts: 195

Re: Proc freq and zeros

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

Super User
Posts: 9,681

Re: Proc freq and zeros

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

Ask a Question
Discussion stats
  • 3 replies
  • 720 views
  • 1 like
  • 4 in conversation