## How to change the number into character format?

Solved
Super Contributor
Posts: 398

# How to change the number into character format?

[ Edited ]

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

Accepted Solutions
Solution
‎04-07-2016 04:58 PM
Super User
Posts: 13,583

## Re: How to change the number into character format?

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.));

All Replies
PROC Star
Posts: 1,317

## Re: How to change the number into character format?

CharVar = put(Value, best.);

Tom

Super Contributor
Posts: 398

## Re: How to change the number into character format?

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

CharVar = put(Value, best.);

Super User
Posts: 23,776

## Re: How to change the number into character format?

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;``````

Super User
Posts: 13,583

## Re: How to change the number into character format?

In light of your other post with the same data&colon;

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

Super Contributor
Posts: 398

## Re: How to change the number into character format?

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;

Super User
Posts: 23,776

## Re: How to change the number into character format?

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.

Solution
‎04-07-2016 04:58 PM
Super User
Posts: 13,583

## Re: How to change the number into character format?

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.));

Super Contributor
Posts: 398

## Re: How to change the number into character format?

Wow, you're super, Ballardw!

🔒 This topic is solved and locked.