BookmarkSubscribeRSS Feed
VCM
Obsidian | Level 7 VCM
Obsidian | Level 7

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

13 REPLIES 13
Astounding
PROC Star

Perhaps:

 

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

 

Or skip creating PUT3 entirely:

 

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

VCM
Obsidian | Level 7 VCM
Obsidian | Level 7
These didn't work; the alignment of the characters in ADDBLANKS is still the same as above. 😞
Astounding
PROC Star

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;

 

VCM
Obsidian | Level 7 VCM
Obsidian | Level 7
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.
Mo_NHS
Fluorite | Level 6
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

Mo_NHS
Fluorite | Level 6

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.

Satish_Parida
Lapis Lazuli | Level 10

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
VCM
Obsidian | Level 7 VCM
Obsidian | Level 7

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.

 

 

Astounding
PROC Star

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.

VCM
Obsidian | Level 7 VCM
Obsidian | Level 7
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.
Astounding
PROC Star

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

ballardw
Super User

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

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 8803 views
  • 0 likes
  • 6 in conversation