BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
SamSays
Calcite | Level 5

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.)

 

snrich_0-1757611310655.png

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.)

snrich_1-1757611618657.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

Tom_0-1757615659863.png

 

 

View solution in original post

6 REPLIES 6
quickbluefish
Barite | Level 11

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;

 

SamSays
Calcite | Level 5

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?

quickbluefish
Barite | Level 11

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

 

Tom
Super User Tom
Super User

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;

Tom_0-1757615659863.png

 

 

FreelanceReinh
Jade | Level 19

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 154 views
  • 0 likes
  • 4 in conversation