Solved
Contributor
Posts: 53

# datatype conversion

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

Accepted Solutions
Solution
‎04-20-2016 03:29 PM
Posts: 1,252

## Re: datatype conversion

[ Edited ]

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

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

All Replies
Super User
Posts: 23,754

## Re: datatype conversion

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.);``
Contributor
Posts: 53

## Re: datatype conversion

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

Super User
Posts: 13,563

## Re: datatype conversion

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.

Solution
‎04-20-2016 03:29 PM
Posts: 1,252

## Re: datatype conversion

[ Edited ]

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

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

Regular Contributor
Posts: 222

## Re: datatype conversion

[ Edited ]

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.

Super User
Posts: 13,563

## Re: datatype conversion

Are you attempting to change variables in an existing data set or create as in 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.

🔒 This topic is solved and locked.