BookmarkSubscribeRSS Feed
Ronein
Onyx | Level 15

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*/

 

4 REPLIES 4
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
RichardAD
Quartz | Level 8

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

 

 

Tom
Super User Tom
Super User

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.

Ksharp
Super User

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;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 555 views
  • 4 likes
  • 5 in conversation