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

Hello

I want to ask about percent format.

The format  PCT1 show .0574%  but I want it to show  0.0574%

The format PCT2 show (.0574%)  but I want it to show (0.0574%)

Data have;
x1=1276;
x2=-1276;
total=2222151;
pct1=x1/total;
pct2=x2/total;
format pct1 pct2 percent8.5;
Run;
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Counting columns is skill and seems to be disappearing.

 


@Ronein wrote:

Hello

I want to ask about percent format.

The format  PCT1 show .0574%  but I want it to show  0.0574%

The format PCT2 show (.0574%)  but I want it to show (0.0574%)

Data have;
x1=1276;
x2=-1276;
total=2222151;
pct1=x1/total;
pct2=x2/total;
format pct1 pct2 percent8.5;
Run;

 (0.0574%) is 9 characters. So does not fit well into any format defined to have a width of 8, i.e. the 8 in percent8.5.

The Decimals of 5 the 5 in percent8.5 exceeds the number of decimals you actually want to show which is 4, 0574. The % symbol is not a decimal in the display. So using any .5 for the decimals may be problematic as to resulting appearances.

Run this and see the results in the log:

 data _null_;
  x=-0.000574;
  put 'Format is percent8.5' x= percent8.5;
  put 'Format is percent9.5' x= percent9.5;
  put 'Format is percent9.4' x= percent9.4;
run;

See what happens with percent9.5? Forcing the extra decimal which is what the .5 does means that you get either and unwanted 0 or other value from rounding your calculated value as the fifth decimal.

 

View solution in original post

4 REPLIES 4
Amir
PROC Star

Hi,

 

Does using format percent9.4 give you what you want, as below?

 

data have;
  x1 = 1276;
  x2 =-1276;
  total = 2222151;
  pct1 = x1/total;
  pct2 = x2/total;
  format pct1 pct2 percent9.4;
run;


proc print data = have;
run;

Amir_0-1749421350230.png

 

 

Thanks & kind regards,

Amir.

Tom
Super User Tom
Super User

Count the characters and set the WIDTH of the format to match.

Also count the number of digits you want after the decimal point and set the DECIMAL places part of the format to match.

 

Notice that when you set too short of a WIDTH then SAS will move the decimal point to make the actual value fit into the space allocated.   That is why you don't get the leading zero in your example.  And also why it does not show you the 5th decimal place you asked for.

 

To show 5 decimal digits with percentages between 0 and 100 you will need 12 characters.

If you only want 4 decimal digits then use 4 instead of 5 and then you can reduce the width from 12 to 11.

(0.0574%)
123456789
   1234

(100.00000%)
123456789012
     12345

.

 

ballardw
Super User

Counting columns is skill and seems to be disappearing.

 


@Ronein wrote:

Hello

I want to ask about percent format.

The format  PCT1 show .0574%  but I want it to show  0.0574%

The format PCT2 show (.0574%)  but I want it to show (0.0574%)

Data have;
x1=1276;
x2=-1276;
total=2222151;
pct1=x1/total;
pct2=x2/total;
format pct1 pct2 percent8.5;
Run;

 (0.0574%) is 9 characters. So does not fit well into any format defined to have a width of 8, i.e. the 8 in percent8.5.

The Decimals of 5 the 5 in percent8.5 exceeds the number of decimals you actually want to show which is 4, 0574. The % symbol is not a decimal in the display. So using any .5 for the decimals may be problematic as to resulting appearances.

Run this and see the results in the log:

 data _null_;
  x=-0.000574;
  put 'Format is percent8.5' x= percent8.5;
  put 'Format is percent9.5' x= percent9.5;
  put 'Format is percent9.4' x= percent9.4;
run;

See what happens with percent9.5? Forcing the extra decimal which is what the .5 does means that you get either and unwanted 0 or other value from rounding your calculated value as the fifth decimal.

 

Kurt_Bremser
Super User
( 0 . 0 5 7 4 % )
1 2 3 4 5 6 7 8 9

Just count, and use the proper number for the fractional digits.

Which tells you the format should be PERCENT9.4

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 4 replies
  • 5558 views
  • 3 likes
  • 5 in conversation