BookmarkSubscribeRSS Feed
Minho_Kang
Calcite | Level 5

I've had a curiosity about code below. And for reference, I've used SAS university version.

DATA dept;
INPUT name $18. +1 hired DATE7.
+1 salary 5. +1 pct PERCENT5.1;
FORMAT hired YYMMDD10. salary DOLLAR9.1
pct PERCENT7.1;
CARDS;
Martin, Virginia   09aug98 34800 12.5%
Singleton, MaryAnn 24apr99 27900 26.3%
Leighton, Maurice  16dec15 32600 18.9%
Freuler, Carl      15feb88 29900 11.2%
Cage, Merce        19oct00 39800 23.1%
;

PROC PRINT DATA=dept;
RUN;

let us look at percent format syntax! In input sentence, PERCENT5.1 is fine in running code, but in format sentence, PERCENT length is at least 7. 

I tried to write PERCENT5.1 in format sentence, but pct values are truncated like 13%, 26%, etc.

 Why are they different? I thought that It isn't problem that Input length and output(format) length are same. So, could you fix my error of thought? 

3 REPLIES 3
Patrick
Opal | Level 21

I like your question and it made me consult the documentation. What I found here for format PERCENTw.d

Patrick_0-1618715531048.png

It appears the length you need to define as w must also leave space for two potential parentheses.

 

The code you've posted also touches on something else that's important to know.

The first PERCENTw.you're using is a informat documented here. What you find there is:

Patrick_1-1618716046359.png

 

Above is something that applies for all numerical informats and it's something that's not very intuitive to me - and I've also seen other people stepping into this "trap".

What above statement actually means: If you define a value for d but your source string does not contain a dot with a decimal portion then SAS will divide the source value by 10 power d when reading it into SAS. That's something you very rarely want to happen.

As a rule of thumb: Never define a d value in an informat unless you have a very good reason to do so. For your code just use:

input ... pct PERCENT5.

To illustrate the behavior when defining a d value check-out what happens to the pct value in the last row.

DATA dept;
  INPUT name $18. +1 hired DATE7.
    +1 salary 5. +1 pct PERCENT5.1;
  FORMAT hired YYMMDD10. salary DOLLAR9.1
    pct PERCENT7.1;
  CARDS;
Martin, Virginia   09aug98 34800 12.5%
Singleton, MaryAnn 24apr99 27900 26.3%
Leighton, Maurice  16dec15 32600 18.9%
Freuler, Carl      15feb88 29900 11.2%
Cage, Merce        19oct00 39800 23.1%
Cage, Merce        19oct00 39800 23%
;

PROC PRINT DATA=dept;
RUN;

Patrick_2-1618716454769.png

 

 

 

AmeeKang
Quartz | Level 8

The percent syntax express a given number as a percentage.

In percet w.d , w means total length and d means decimal parts.

w must be greater than 3. Because, '%' occupies 3 digits.

ballardw
Super User

@AmeeKang wrote:

The percent syntax express a given number as a percentage.

In percet w.d , w means total length and d means decimal parts.

w must be greater than 3. Because, '%' occupies 3 digits.


More because "(%)" occupies 3 digits. The Percent format to display negative values uses () around the value, so you need to accommodate those even if there are no negative values in your data. (1.2%) is how the format displays -1.2% 

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

Discussion stats
  • 3 replies
  • 1648 views
  • 2 likes
  • 4 in conversation