turn on suggestions

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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- datatype conversion

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 10:01 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 11:45 AM - edited 04-15-2016 11:49 AM

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 10:31 AM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 11:00 AM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 11:23 AM

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 11:45 AM - edited 04-15-2016 11:49 AM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 10:31 AM - edited 04-15-2016 01:38 PM

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
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-15-2016 10:34 AM

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.