BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

Hello:

 

I have the following number.  They are in numberic in SAS.  I would like to change them into Charateric format.  I would like to use Interger format.  How to use put statement?  The variable name is Value.   Thanks.

 

 

47989
680
31
681
187
5814
405
319
149
887
5100
4047
74
33
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Because you do not specify a length for the variable formatted_value it gets one assigned based on the first use, Put using 5.1 so the length is 5.

The default behavior with the BEST. format creates a 12 character result that is RIGHT justified. So only the first 5 characters can be stored.

If your value does not have more than 7 charaters to display with the BEST. format then it appears blank because only the first 5 are kept.

Also if you have a large enough value you will get scientific notation.

See this code for examples.

data junk;
   input type $ value1;
   if type in ('O','C','H') then formatted_value=put(value1,5.1);
   if type in ('N','D') then formatted_value=put(value1,best.);
   if type in ('N','D') then otherformatted_value=put(value1,best.);
datalines;
O 25
O 23456789
N 29 
N 1234567
;
run;


proc print; run;

Solution is to place a LENGTH statement before the first attempt AND to left justify the results

 

 

Length Formatted_Value $ 12. ;

if type in ('O','C','H') then formatted_value= strip(put(value1,5.1));

if type in ('N','D') then formatted_value= strip(put(value1,best.));

View solution in original post

8 REPLIES 8
TomKari
Onyx | Level 15

CharVar = put(Value, best.);

 

Tom

ybz12003
Rhodochrosite | Level 12

I use the code below,  It didn't work, I found all the cells are blank in CharVar, 

 

CharVar = put(Value, best.);

Reeza
Super User

Then you're not telling us everything or did it wrong. Post proc contents as indicated by @ballardw, as well as your code and log.

The demo below show's that this method works when the data is numeric as indicated.


data have;
input value;
CharVar = put(Value, best.);
cards;
47989
680
31
681
187
5814
405
319
149
887
5100
4047
74
33
;
run;

proc print data=have;
run;

proc contents data=have;
run;

 

 

 

 

ballardw
Super User

In light of your other post with the same data:

Please run PROC CONTENTS on the data set and show the results here.

ybz12003
Rhodochrosite | Level 12

I wrote the code below, please see my attachment.  All of the numbers are gone in Formatted_value.

 

data NHSS6;
SET NHSS5;
if type in ('O','C','H') then formatted_value=put(value1,5.1);
if type in ('N','D') then formatted_value=put(value1,best.);
run;

Reeza
Super User

It works for some, not all, which means your IF condition is specified incorrectly NOT the conversion. 

 

Check the conversion alone and you'll see it works. Fix your IF condition.

ballardw
Super User

Because you do not specify a length for the variable formatted_value it gets one assigned based on the first use, Put using 5.1 so the length is 5.

The default behavior with the BEST. format creates a 12 character result that is RIGHT justified. So only the first 5 characters can be stored.

If your value does not have more than 7 charaters to display with the BEST. format then it appears blank because only the first 5 are kept.

Also if you have a large enough value you will get scientific notation.

See this code for examples.

data junk;
   input type $ value1;
   if type in ('O','C','H') then formatted_value=put(value1,5.1);
   if type in ('N','D') then formatted_value=put(value1,best.);
   if type in ('N','D') then otherformatted_value=put(value1,best.);
datalines;
O 25
O 23456789
N 29 
N 1234567
;
run;


proc print; run;

Solution is to place a LENGTH statement before the first attempt AND to left justify the results

 

 

Length Formatted_Value $ 12. ;

if type in ('O','C','H') then formatted_value= strip(put(value1,5.1));

if type in ('N','D') then formatted_value= strip(put(value1,best.));

ybz12003
Rhodochrosite | Level 12

Wow, you're super, Ballardw!  

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 1519 views
  • 0 likes
  • 4 in conversation