- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I like your question and it made me consult the documentation. What I found here for format PERCENTw.d
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.d you're using is a informat documented here. What you find there is:
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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%