05-13-2013 02:22 PM
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";
proc freq data=jess.exampledata;
05-13-2013 03:33 PM
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;
1 = '1' 2='2' 3='3' 4='4' 5='5' 6='6' 7='7' 8='8' 9='9';
proc tabulate data=jess.exampledata;
class wsb6 / preloadfmt order=data;
format wsb6 wsb.;
table wsb6, n pctn / printmiss misstext='0';
Should do something close to what you want.
05-14-2013 03:14 AM
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);
do i = 1 to 9;
create table final as
when a.var ^= i then i
end as var,
when count = . then 0
else count = count
end as count
from test1 as a right join
test2 as b
on a.var = b.i;
You need to convert your variable to Numeric if it is charcter...and then run the above code...
05-14-2013 10:19 AM
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;