DATA Step, Macro, Functions and more

Forcing alignment of numeric-turned-character variables

Reply
Contributor VCM
Contributor
Posts: 21

Forcing alignment of numeric-turned-character variables

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 ;-):

 

alignment.png

 

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.

 

output.png

 

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

Super User
Posts: 6,629

Re: Forcing alignment of numeric-turned-character variables

Perhaps:

 

addblanks = '(' || put3 || ')';

 

Or skip creating PUT3 entirely:

 

addblanks = '(' || put(c1, 3.) || ')';

Contributor VCM
Contributor
Posts: 21

Re: Forcing alignment of numeric-turned-character variables

Posted in reply to Astounding
These didn't work; the alignment of the characters in ADDBLANKS is still the same as above. Smiley Sad
Super User
Posts: 6,629

Re: Forcing alignment of numeric-turned-character variables

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;

 

Contributor VCM
Contributor
Posts: 21

Re: Forcing alignment of numeric-turned-character variables

Posted in reply to Astounding
Thanks, but I'm not worried about the numbers in parentheses; I need to align the 3-digit/character number in front of the parentheses.
Occasional Contributor
Posts: 8

Re: Forcing alignment of numeric-turned-character variables

[ Edited ]
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:

z.JPG

Occasional Contributor
Posts: 8

Re: Forcing alignment of numeric-turned-character variables

[ Edited ]

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:

 

z.JPG

 

 

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.

Frequent Contributor
Posts: 109

Re: Forcing alignment of numeric-turned-character variables

[ Edited ]

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.

Super User
Super User
Posts: 9,407

Re: Forcing alignment of numeric-turned-character variables

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;
Contributor VCM
Contributor
Posts: 21

Re: Forcing alignment of numeric-turned-character variables

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.

 

 

Super User
Posts: 6,629

Re: Forcing alignment of numeric-turned-character variables

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.

Contributor VCM
Contributor
Posts: 21

Re: Forcing alignment of numeric-turned-character variables

Posted in reply to Astounding
That's why I added
STRIPPUT3 = strip(PUT3);
LEN = length(STRIPPUT3);

You can see in the original post that STRIPPUT3 does left-align and the lengths are correct. But when I add 2 leading blanks to "3" and one leading blank to "82", you can see it still doesn't align correctly.
Super User
Posts: 6,629

Re: Forcing alignment of numeric-turned-character variables

[ Edited ]

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.;

Super User
Posts: 13,304

Re: Forcing alignment of numeric-turned-character variables

[ Edited ]

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

 

Ask a Question
Discussion stats
  • 13 replies
  • 165 views
  • 0 likes
  • 6 in conversation