BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SanderEhmsen
Quartz | Level 8

Hi all

 

The percentn-format is a tricky one for me to understand. 

 

Percentn10.2 where 10 is called the first parameter and 2 is called the second

 

I have experienced just to use a fairly wide first parameter and then specify the last parameter according to my need. 

 

But why is that? 

If I do the following:

 

 

data test2;
length format $50.;
length var $50.;
format='percentn4.0'; var=put(0.10412456435, percentn4.0); output;
format='percentn4.1'; var=put(0.10412456435, percentn4.1); output;
format='percentn4.2'; var=put(0.10412456435, percentn4.2); output;
format='percentn4.3'; var=put(0.10412456435, percentn4.3); output;
format='percentn5.0'; var=put(0.10412456435, percentn5.0); output;
format='percentn5.1'; var=put(0.10412456435, percentn5.1); output;
format='percentn5.2'; var=put(0.10412456435, percentn5.2); output;
format='percentn5.3'; var=put(0.10412456435, percentn5.3); output;
format='percentn5.4'; var=put(0.10412456435, percentn5.4); output;
format='percentn6.0'; var=put(0.10412456435, percentn6.0); output;
format='percentn6.1'; var=put(0.10412456435, percentn6.1); output;
format='percentn6.2'; var=put(0.10412456435, percentn6.2); output;
format='percentn6.3'; var=put(0.10412456435, percentn6.3); output;
format='percentn6.4'; var=put(0.10412456435, percentn6.4); output;
format='percentn6.5'; var=put(0.10412456435, percentn6.5); output;
format='percentn7.0'; var=put(0.10412456435, percentn7.0); output;
format='percentn7.1'; var=put(0.10412456435, percentn7.1); output;
format='percentn7.2'; var=put(0.10412456435, percentn7.2); output;
format='percentn7.3'; var=put(0.10412456435, percentn7.3); output;
format='percentn7.4'; var=put(0.10412456435, percentn7.4); output;
format='percentn7.5'; var=put(0.10412456435, percentn7.5); output;
format='percentn7.6'; var=put(0.10412456435, percentn7.6); output;
format='percentn8.0'; var=put(0.10412456435, percentn8.0); output;
RUN;

 

Why is percent4.0 not giving me 10% instead of *%. 

I guess the dot (.) and % are included in the first parameter. But still. 

The way I have come to live with it is by assuming the % consist of two dots and a slash. Then it makes sense. But I cannot be what is going on.

 

Can anyone help a troubled soul?

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Your code shows use of PerctNw.d format but your question asks about Percentw.d. They are not the same in behavior.

 

The Percentw.d uses parentheses around values to indicate negative values. So two of the 4 positions are reserved for those when they might appear which leaves only 2 of 4 to display the value. One more position gets taken by the % sign. So there is only one character position for the numeric value. Which means it can only display 1 digit and since 10% can not be displayed with 1 digit you get the *, which is generally SAS's way of telling you that your format is not wide enough to display.

 

The case is similar when you use PercentNw.d. The PercentN will show negative values with a minus sign preceding, so the format needs one character for that plus one for the % and a trailing blank.(never really got why, but that's in the documentation) So again leaves only one space for the value and 10 won't fit in the remaining 1 character with percentn4.

When you start specifying decimal more characters are reserved for them and you can run of digits quickly. Any format with a width of 4 and 3 decimal places won't show any integer portion because you asked for 3 decimal positions plus a decimal point. And that's before you get into the minus sign the format will reserve without showing.

 

Your log shows a note like this because of attempting to fit values into to little space.

NOTE: At least one W.D format was too small for the number to be printed. The decimal may be
      shifted by the "BEST" format.

The order of when which spaces get reserved for which part of the rule seem to be a bit odd when the percent and percentn formats are used. So if I want to show 2 decimals I start with Percent8.2 but that will have issues if the value is 10 or greater (1000%)

View solution in original post

1 REPLY 1
ballardw
Super User

Your code shows use of PerctNw.d format but your question asks about Percentw.d. They are not the same in behavior.

 

The Percentw.d uses parentheses around values to indicate negative values. So two of the 4 positions are reserved for those when they might appear which leaves only 2 of 4 to display the value. One more position gets taken by the % sign. So there is only one character position for the numeric value. Which means it can only display 1 digit and since 10% can not be displayed with 1 digit you get the *, which is generally SAS's way of telling you that your format is not wide enough to display.

 

The case is similar when you use PercentNw.d. The PercentN will show negative values with a minus sign preceding, so the format needs one character for that plus one for the % and a trailing blank.(never really got why, but that's in the documentation) So again leaves only one space for the value and 10 won't fit in the remaining 1 character with percentn4.

When you start specifying decimal more characters are reserved for them and you can run of digits quickly. Any format with a width of 4 and 3 decimal places won't show any integer portion because you asked for 3 decimal positions plus a decimal point. And that's before you get into the minus sign the format will reserve without showing.

 

Your log shows a note like this because of attempting to fit values into to little space.

NOTE: At least one W.D format was too small for the number to be printed. The decimal may be
      shifted by the "BEST" format.

The order of when which spaces get reserved for which part of the rule seem to be a bit odd when the percent and percentn formats are used. So if I want to show 2 decimals I start with Percent8.2 but that will have issues if the value is 10 or greater (1000%)

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 845 views
  • 1 like
  • 2 in conversation