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

HI,

 

I have a dataset.

 

Data a;

var1 var2 var3;

RUN;

 

var1,2,3 are numeric. How to I convert their datatype to be character?

 

Thanks,

Archana

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hi  Archana,

 

I'm not sure what "differences" you refer to.

 

Please note that BESTw. format always results in a string of length ("width") w (here: w=10). These w characters can include

  • leading blanks
  • a minus sign
  • digits left of the decimal point
  • a decimal point
  • digits right of the decimal point
  • exponential notation like "E-6" for 10^-6

If you want to obtain a fixed number of decimal places (which you didn't tell in your initial post), BESTw. is inappropriate. Use the w.d format instead, with d=4 and a sufficiently large w, for example w=17 (this will most likely be larger than necessary).

 

Var1_char = put(var1, 17.4);

 

View solution in original post

6 REPLIES 6
Reeza
Super User

Use a PUT() statement. 

Make sure to use appropriate format, best32 is an example. 

 

Also note that you cannot change a variables type so you have to create a new variable. 

 

Var1_char = put(var1, best32.);
ArchanaSudhir
Obsidian | Level 7

Thanks.

 

But I am getting differences by using best10.

Var4 var5
0.99079152 0.999999813
   
Var4 var5
0.9908 1.0000

 

I should get the answer in 4 decimal places.

How do I get it?

 

Thanks,

Archana

ballardw
Super User

@ArchanaSudhir wrote:

Thanks.

 

But I am getting differences by using best10.

Var4 var5
0.99079152 0.999999813
   
Var4 var5
0.9908 1.0000

 

I should get the answer in 4 decimal places.

How do I get it?

 

Thanks,

Archana


Pick an appropriate format. If you require a specific number of decimal places then you need to provide a format that will force them. Without knowing ANY details of your data or output requirement you get a generic response. Try

Put(var,f10.4);

The number after the f has to be large enough to accomate the largest value including 4 spaces for decimal values and 1 for the decimal itself. So if you have values like 1234567.8787 you would need f12.4. But don't expect us to guess correctly without all of the details.

FreelanceReinh
Jade | Level 19

Hi  Archana,

 

I'm not sure what "differences" you refer to.

 

Please note that BESTw. format always results in a string of length ("width") w (here: w=10). These w characters can include

  • leading blanks
  • a minus sign
  • digits left of the decimal point
  • a decimal point
  • digits right of the decimal point
  • exponential notation like "E-6" for 10^-6

If you want to obtain a fixed number of decimal places (which you didn't tell in your initial post), BESTw. is inappropriate. Use the w.d format instead, with d=4 and a sufficiently large w, for example w=17 (this will most likely be larger than necessary).

 

Var1_char = put(var1, 17.4);

 

DartRodrigo
Lapis Lazuli | Level 10

Hi mate,

 

Just setup a new variable name and use put(var1,best10.);

 

cha1 = put(var1,best10.);
cha2 = put(var2,best10.);
cha3 = put(var3,best10.);

That's all

 

One more advice: put() is from numeric to character and input is the opposite.

 

Hope this helps

But remember the format must fit the size of your number, that's why i used the best10., if your number have more than 10 number before the comma, use a greater number format.

ballardw
Super User

Are you attempting to change variables in an existing data set or create as in your example?

 

With your "example"

data a;

   length Var1 Var2 Var3 $ 10; /* specify number to be largest number of characters you'll need the variable to hold*/

run;

 

If in an existing data set, while learing SAS the best idea would be to make a new dataset, rename your old variables (data set option) and use something like Var1 = put(OldVar1,best12.); pick an appropriate format for the appearance you want.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 6 replies
  • 1734 views
  • 0 likes
  • 5 in conversation