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.)
VCM
Perhaps:
addblanks = '(' || put3 || ')';
Or skip creating PUT3 entirely:
addblanks = '(' || put(c1, 3.) || ')';
It worked for me (see sample program below).
If it's not working for you, you might have to show your log.
data test;
input c;
put3 = put(c, 3.);
addblanks = '(' || put3 || ')';
datalines;
3
82
104
;
proc print; run;
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;
Output:
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;
Log
c1=104 c2=2 z1=00000104 z2=00000002 put1=00000104 put2=00000002 string1=1 4 string2=2 _ERROR_=0 _N_=1
Output:
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.
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.
Thank you.
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;
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.
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.
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.;
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
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!
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.