Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- datatype conversion

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-15-2016 10:01 AM
(1157 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi Archana,

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

Please note that BEST*w*. format always results in a string of length ("*w*idth") *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), BEST*w*. 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);`

6 REPLIES 6

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

@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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Hi Archana,

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

Please note that BEST*w*. format always results in a string of length ("*w*idth") *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), BEST*w*. 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);`

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.