02-15-2018 07:59 AM
I'm creating a table with counts + percentages in this format: nnn (nnn.n)
I want them to line up like this; numbers are made up, so no digs at my math skills ;-):
The percentages in the parentheses are coming out fine, but I can't force the counts (before the parentheses) to align properly once I change them to character.
C1 is the original numeric variable.
PUT3 = put(C1,3.);
STRIPPUT3 = strip(PUT3);
LEN = length(STRIPPUT3);
if LEN = 1 then addblanks = " "||compress(stripput3); **2 blanks added in front**;
if LEN = 2 then addblanks = " "||compress(stripput3); **1 blank added in front**;
if LEN = 3 then addblanks = compress(stripput3);
Even adding leading blanks doesn't align the character values the way the numeric values are. Similar logic applied to the percentages works beautifully.
This is driving me nuts; what am I missing? (Disclaimer: I have not had my coffee yet.)
02-15-2018 09:43 AM
It worked for me (see sample program below).
If it's not working for you, you might have to show your log.
put3 = put(c, 3.);
addblanks = '(' || put3 || ')';
proc print; run;
02-15-2018 10:15 AM
02-15-2018 10:43 AM - edited 02-15-2018 10:45 AM
data test; /*create two value*/ c1=104; c2=2; /*use right alignment*/ s1=right(put(c1,8.))||" (99.00)"; s2=right(put(c2,8.))||" (99.00)"; output; /*create another row*/ c1=2; c2=104; s1=right(put(c1,8.))||" (99.00)"; s2=right(put(c2,8.))||" (99.00)"; output; put _ALL_; run;
02-15-2018 08:32 AM - edited 02-15-2018 08:35 AM
you can either:
1. Use a leading zeros format or leading spaces (replaces the 0 with space):
data test; /*create two value*/ c1=104; c2=2; /*add leading zeros*/ z1=c1; z2=c2; put1=put(c1,z8.); put2=put(c2,z8.); /*add leadin spaces*/ string1=tranwrd(put(c1,z8.),'0',' '); string2=tranwrd(put(c2,z8.),'0',' '); format z1 z8. z2 z8. ; put _ALL_; run;
c1=104 c2=2 z1=00000104 z2=00000002 put1=00000104 put2=00000002 string1=1 4 string2=2 _ERROR_=0 _N_=1
if you want this field to be character with leading spaces instead of 0
2. use the alignment / justify option in proc print; please refer to this post.
02-15-2018 08:34 AM - edited 02-15-2018 08:36 AM
Please clear us on
1. What your data should look like.
2. What your data looks like now.
3, Source data
4. Codes you have written.
02-15-2018 09:21 AM
Would not the right function do what you want, assuming you always have 1 decimal place:
data want; length b $10.; a=0.1; b=cat("(",right(put(a,5.1)),")"); output; a=123.1; b=cat("(",right(put(a,5.1)),")"); output; run;
02-15-2018 10:13 AM
I'm not trying to align the numbers within the parentheses--those are coming out fine. I'm trying to align the numbers in front of the parentheses.
I thought that if you put(number,format.) to create a character variable, it would automatically left-align it (then all I would have to do is add leading blanks where needed), but that doesn't seem to be the case.
02-15-2018 10:18 AM
No, the leading blanks are not removed. This should be fine:
put3 = put(c1, 3.);
PUT3 does contain leading blanks. If you don't see them, it would be related to how you are viewing (or printing) PUT3.
02-15-2018 10:23 AM
02-15-2018 10:29 AM - edited 02-15-2018 10:31 AM
Doesn't align correctly when? When you run a PROC PRINT? Using which font? When you run some other procedure? When you export the data to Excel? Do you have a log that shows how you got the non-aligned output?
Perhaps all you are missing is a format statement:
format put3 $char3.;
02-15-2018 10:41 AM - edited 02-15-2018 05:30 PM
Since the only thing that should worry about things like decimal alignment are people then the changing data values is NOT the way to approach this. In general the output report generators like Proc Print, Tabulate or Report will right align values which means decimal points won't align but any integer values will.
SAS supplies several formats that will align decimals on output such as BESTDw.p or Dw.p. Use one of these in printed or ODS output.
And many times your "failure" to force alignment of a character value will occur because of proportional fonts and the way they treat multiple spaces and the width of characters displaye