Hello
I wan to convert numeric to char.
I want to see 2 figures after point
so see 0.18 and not 0.1818181818
what is the way to do it pls?
data have;
input x w;
cards;
10 20
5 10
2 11
;
run;
data want;
set have;
z=x/w;
z_char=put(z,best.);
format z 8.2;
run;
/*0.1818181818*/
If you change
z_char=put(z,best.);
to
z_char=put(z,8.2);
then z_char will display what you want. Assuming this statement is the first reference to z_char, it will be an 8-byte character variable. It will also be right justified, and with two decimal places.
You may not want it to be right justified, in which case, you can use
z_char=left(put(z,8.2));
But remember that if you left justify the character value, then sorting by z_char may not generate the same order as sorting by z (i.e. '11.18 ' would precede '6.18 ').
--- Lexicographic ordering.
You can use VVALUE to retrieve the formatted representation of a variable's value using the actively associated format.
Did you know you can specify -L on a format to indicate left justification?
data _null_; x = 1/3; y = x ; format x 8.2 y 8.2-L ; x_char = vvalue(x) ; y_char = vvalue(y) ; put '!' x_char $CHAR10. ; put '!' y_char $CHAR10. ; run ;
Log
! 0.33 <--- x_char !0.33 <--- y_char
Why did you ask SAS to PRINT the values of Z using the 8.2 format but then ask it to create Z_CHAR using the BEST. format?
Remember that a FORMAT is instructions for how to convert values into text. It does not change how the value is stored, just how it is displayed.
So you want truncate it or round it .
if you want round it :
data have;
input x w;
cards;
10 20
5 10
2 11
;
run;
data want;
set have;
z=x/w;
z_char=put(z,12.2 -l);
run;
If you want truncate it:
data have;
input x w;
cards;
10 20
5 10
2 11
;
run;
proc format;
picture fmt
low-high='0009.99'
;
run;
data want;
set have;
z=x/w;
z_char=put(z,fmt. -l);
run;
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.