Hi friends,
I'm working with survey data. For my Q2 I start with a frequency table that counts and creates a percentage. (Note that it's the real percentage already multiplied by 100.)
In my proc report, I want the percentage rounded so I included format=8.1. However, I want my final report to have actual percentage signs. I think I essentially want a double format. What can I do to have my cake and eat it too? I don't think I can use the percent format here. I've tried one of those picture formats (low-high) but it doesn't work because I also want it to round.
proc freq data=Lib.Q2Response noprint;
tables Q2 / missing out=Lib.Q2_freq nocol nocum all;
run;
proc report data=Lib.Q2_freq nowd
style(header)=[color = cxCC0000];
columns Q2 Count percent Q2=Avg;
define Q2 / left display "Response" width=45;
define percent / center analysis sum "%" format=8.1 width=10;
define Count/center analysis sum "N" width=10 ;
define Avg/ center analysis mean weight=Count noprint;
rbreak after / summarize
style(summary)=[color = cxCC0000 font_weight=bold just=center];
compute after / style =[just=left color =cxCC0000 fontstyle=italic];
line 'Mean Rating' +145 Avg 8.2;
endcomp;
Compute before _page_ / style =[just=center fontsize=10pt fontweight=bold] ;
line "TITLE OF MY TABLE";
endcomp;
run;
(I want my table to say 3.8%, etc.)
As others have said why not just add a step to divide by 100?
If you did want to do it in the PROC REPORT step then make a new computed variable and divide it there. I had to force the sum to 1 to get the summary line to print, not sure why.
proc freq data=sashelp.class;
tables age / noprint missing out=counts nocol nocum ;
run;
proc report data=counts nowd
style(header)=[color = cxCC0000]
;
columns age Count percent percent=p2 age=Avg;
define age / left display "Age" ;
define percent / display noprint;
define p2 / computed center analysis sum "%" format=percent8.1 width=10;
define Count/center analysis sum "N" width=10 ;
define Avg/ center analysis mean weight=Count noprint;
rbreak after / summarize
style(summary)=[color = cxCC0000 font_weight=bold just=center]
;
compute p2 ;
p2 = percent/100 ;
endcomp;
compute after / style =[just=left color =cxCC0000 fontstyle=italic];
p2=1;
line 'Mean Age' +145 Avg 8.2;
endcomp;
Compute before _page_ / style =[just=center fontsize=10pt fontweight=bold] ;
line "TITLE OF MY TABLE";
endcomp;
run;
In general, it's best to *not* multiply the value by 100 and instead just use the built-in SAS 'percent' format. To round to one decimal place, just use:
format your_variable percent8.1;
But my proc feq is creating my percentage. It's doing that for me and I don't know how to get it to not do that. Do you know?
I'll have to defer to others - I have done my best to avoid PROC REPORT and so really can't advise here. But you could try changing the PROC FREQ output dataset 'lib.Q2_Freq' as follows:
data lib.q2_freq;
set lib.q2_freq;
percent=percent/100;
run;
...and then, in the PROC REPORT, on the line where you currently have
format=8.1
...change to:
format=percent8.1
As others have said why not just add a step to divide by 100?
If you did want to do it in the PROC REPORT step then make a new computed variable and divide it there. I had to force the sum to 1 to get the summary line to print, not sure why.
proc freq data=sashelp.class;
tables age / noprint missing out=counts nocol nocum ;
run;
proc report data=counts nowd
style(header)=[color = cxCC0000]
;
columns age Count percent percent=p2 age=Avg;
define age / left display "Age" ;
define percent / display noprint;
define p2 / computed center analysis sum "%" format=percent8.1 width=10;
define Count/center analysis sum "N" width=10 ;
define Avg/ center analysis mean weight=Count noprint;
rbreak after / summarize
style(summary)=[color = cxCC0000 font_weight=bold just=center]
;
compute p2 ;
p2 = percent/100 ;
endcomp;
compute after / style =[just=left color =cxCC0000 fontstyle=italic];
p2=1;
line 'Mean Age' +145 Avg 8.2;
endcomp;
Compute before _page_ / style =[just=center fontsize=10pt fontweight=bold] ;
line "TITLE OF MY TABLE";
endcomp;
run;
Thanks, Tom!
Hi @SamSays,
@SamSays wrote:
I've tried one of those picture formats (low-high) but it doesn't work because I also want it to round.
Picture formats do round if you use the ROUND option:
proc format; picture pctfmt (round) 0-high='0009.9%'; run;
As demonstrated below, this picture format displays non-negative values x<999.95 exactly like the PERCENT8.1 format displays x/100.
247 data chk; 248 do n=0 to 999949; 249 x=n/1000; 250 p1=put(x, pctfmt.); 251 p2=put(x/100, percent8.1); 252 if p1 ne p2 then output; 253 end; 254 run; NOTE: The data set WORK.CHK has 0 observations and 4 variables.
However, the PERCENT8.1 format adds an unnecessary trailing blank, so that variable p2 has length 8, whereas p1 has length 7, which is the default length of the format. Unlike PERCENT8.1, the picture format would also display larger values up to 9999.949... correctly.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.