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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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