DATA Step, Macro, Functions and more

Percentage display in datastep

Reply
Frequent Contributor
Posts: 131

Percentage display in datastep

data app_summary02;

set app_summary2;

pct2=round(pct);

run;

 

rounded output example

100

50

70

 

 

 

data app_summary2;

set app_summary02;

format pct percent4.;

pct=pct2;

drop pct2;

run;

 

I want to format the percent as such however its not formatting correctly

Desired result

100%

50%

70%

Valued Guide
Posts: 505

Re: Percentage display in datastep

This seems to work for me

HAVE

Up to 40 obs WORK.HAVE total obs=3

Obs            PCT2

 1            0.101
 2            0.201
 3            0.301

WANT

Obs    PCT

 1     10%
 2     20%
 3     30%


SOLUTION

* create some data;
data have;
 do pct2=.101 to .301 by .1;
    output;
 end;
run;quit;

* format new variable;
data want;
set have;
format pct percent.;
pct=pct2;
drop pct2;
run;

* print with format;
proc print data=want;
run;quit;


Super User
Posts: 5,083

Re: Percentage display in datastep

[ Edited ]

This response points out one of the issues with the percent format.  It expects to see values between 0 and 1, not between 0 and 100.  So you need to adjust your values accordingly.  Values greater than 1 are permitted, so 1.5 prints as 150%.

 

Another issue is that the percent format adds a leading and a trailing blank.  That happens because it prints negative numbers in parentheses rather than using a negative sign.  So you need to allow enough room in the format width for those characters.  If that is less than ideal, you can switch to the PERCENTN format instead of the PERCENT format.  No trailing blank is needed, and negative numbers print with a negative sign.

Super User
Posts: 17,831

Re: Percentage display in datastep

Another option is to create your own format that handles whole numbers. 

This is a common question since both PROC TABULATE/FREQ report percents between 0 and 100. 

 

The Code tab has the code for a custom format

http://support.sas.com/kb/38/001.html

Ask a Question
Discussion stats
  • 3 replies
  • 173 views
  • 0 likes
  • 4 in conversation