Hi,
I am trying to center alignment in data set for column have number & percent
I am using below code to generate respective percentages
varout=strip(put(n,12.))||' ('||strip(put((n*100/denom),5.1))||')' ;
this is sample data
data test ;
input col1 ;
cards;
6
6 (100.0)
5 ( 83.3)
0
868 ( 35.1)
1607
1607 (100.0)
475 ( 29.6)
14 ( 0.9)
;
data test2 ;
input col2 $17. ;
cards;
6 (100.0)
5 ( 83.3)
0
868 ( 35.1)
1607
1607 (100.0)
475 ( 29.6)
14 ( 0.9)
;
I am try to alignment in center with decimal in line and numbers as showed in second image
My out put is like below in data set ;
i want output like below in data set : how i can adjust spaces and alignment in as below
If you want the spaces then why are you calling the STRIP() function to remove them?
Thank you . I have tried with strip removing but it didn't work
if i want assign space according decimal point , decimal point will come as center as shown below
53
8.1
8.3
40
7.8
29.07
51
I would like to assign the spaces according to decimal positions like bellow without changing decimal values.
xxx |
x.x |
x.xx |
x.x |
x |
xx |
xxxx |
x.x |
x.xx |
x.xxxx |
xx |
xx |
Please check the following code (Check log for out put)
data have;
input decimals;
cards;
100
1.2
23.12
43.569
209.09
0987.0987
900.900
00
5
.509
.3
0.88
100001.6009
;
run;
data _null_;
set have;
retain max_bd max_ad;
chars=strip(put(decimals,best32. -l));
d=findc(chars,'.');
l=length(strip(chars));
if d ne 0 then do;
bd=substr(chars,1,d-1);
ad=substr(chars,d+1,l-(d));
end;
else do;
bd=substr(chars,1,l);
ad='';
end;
max_bd=max(max_bd,length(bd));
max_ad=max(max_ad,length(ad));
call symput('max_bd',max_bd);
call symput('max_ad',max_ad);
run;
%put &=max_bd.;
data want(drop= d l chars);
set have;
chars=strip(put(decimals,best32. -l));
d=findc(strip(chars),'.');
l=length(strip(chars));
if d ne 0 then d=d-1;
if d = 0 then d=l;
need='|'||repeat(' ',&max_bd.-d-1+4)||chars; /*Here 4 is the number of needed spaces*/
put need=;
run;
Please let us know if it word for you.
Hello
I have tried your code and it's not working as expected .
i need to align decimals in one line for reporting purpose so need to adjust space , placed screen shot here below
and also data pasted here
121
6.02
42
42
10.08
67
31
7.08
88
122
3.24
3.24
10.44
2.88
3.24
3.24
78
89
92
78
85
9.72
11
104
92
64
7.2
If you have numbers then just use a format to generate the decimal point where you want it.
If you have information in advance about what format to use for which value then you can use the PUTN() function to make the format variable. Or if you just want to remove the .00 from the end of integer values you could post-process the generate strings.
data have;
input n @@;
cards;
121 6.02 42 42 10.08 67 31 7.08 88 122 3.24 3.24 10.44
2.88 3.24 3.24 78 89 92 78 85 9.72 11 104 92 64 7.2
;
data want ;
set have ;
varout = put(n,10.2);
varout = tranwrd(varout,'.00',' ');
format varout $char10.;
run;
proc print;
run;
Obs n varout 1 121.00 121 2 6.02 6.02 3 42.00 42 4 42.00 42 5 10.08 10.08 6 67.00 67 7 31.00 31 8 7.08 7.08 9 88.00 88 10 122.00 122 11 3.24 3.24 12 3.24 3.24 13 10.44 10.44 14 2.88 2.88 15 3.24 3.24 16 3.24 3.24 17 78.00 78 18 89.00 89 19 92.00 92 20 78.00 78 21 85.00 85 22 9.72 9.72 23 11.00 11 24 104.00 104 25 92.00 92 26 64.00 64 27 7.20 7.20
If you have strings then locate the decimal point and add the appropriate number of leading spaces.
data have;
input str :$10. @@;
cards;
121 6.02 42 42 10.08 67 31 7.08 88 122 3.24 3.24 10.44
2.88 3.24 3.24 78 89 92 78 85 9.72 11 104 92 64 7.2
;
data want ;
set have ;
length varout $10.;
format varout $char10.;
loc = index(str,'.');
if loc=0 then loc=length(str)+1;
varout=str ;
if loc < 8 then varout=cat(repeat(' ',7-loc),varout);
run;
proc print;
run;
Obs str varout loc 1 121 121 4 2 6.02 6.02 2 3 42 42 3 4 42 42 3 5 10.08 10.08 3 6 67 67 3 7 31 31 3 8 7.08 7.08 2 9 88 88 3 10 122 122 4 11 3.24 3.24 2 12 3.24 3.24 2 13 10.44 10.44 3 14 2.88 2.88 2 15 3.24 3.24 2 16 3.24 3.24 2 17 78 78 3 18 89 89 3 19 92 92 3 20 78 78 3 21 85 85 3 22 9.72 9.72 2 23 11 11 3 24 104 104 4 25 92 92 3 26 64 64 3 27 7.2 7.2 2
Don't remove the spaces if you want to keep them. Let's use your posted data.
data have ;
input n p ;
length varout $20;
if p then varout=cat(put(n,12.),' (',put(p,5.1),')');
else varout=put(n,12.);
format varout $char20.;
cards;
. .
6 .
6 100.0
5 83.3
0 .
868 35.1
1607 .
1607 100.0
475 29.6
14 0.9
;
proc print;
run;
Which will look fine on normal listing output.
Obs n p varout 1 . . . 2 6 . 6 3 6 100.0 6 (100.0) 4 5 83.3 5 ( 83.3) 5 0 . 0 6 868 35.1 868 ( 35.1) 7 1607 . 1607 8 1607 100.0 1607 (100.0) 9 475 29.6 475 ( 29.6) 10 14 0.9 14 ( 0.9)
If you want the leading spaces to appear on other outputs, like HTML, then you will need to work harder in the reporting step.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.