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
- /
- General Programming
- /
- How to convert large amount of numeric variables t...

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

06-14-2015 01:47 AM

Hello,

I have around 800 numeric variables and want to convert them into character variables. If using PUT function, I have to convert them one by one.

Is there any other way to do it? As using PUT 800 times is not very doable

Many thanks!

Accepted Solutions

Solution

06-15-2015
10:00 AM

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

06-15-2015 10:00 AM

Try forcing the lengths of your variables to match the length of your format. For example, to create new variables that are 16 characters long:

array chars (800) $ 16 char1-char800;

Then convert numerics to a 16-character format:

chars(i) = put(nums(i), best16.);

You don't have to pick 16 characters, but you should match the lengths and the formats.

Good luck.

All Replies

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

06-14-2015 02:19 AM

Since you have not provided dummy data, here is what i am expecting which you can try . I used arrays

data want;

set have;

array nums(*) num1-num800;

array chars(*) $ char1-char800;

do i = 1 to dim(nums);

chars(i)=put(nums(i),best.);

end;

run;

Thanks,

Jag

Thanks,

Jag

Jag

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

06-14-2015 02:27 AM

Alternatively also try the do over with arrays

data want;

set have;

array nums num1-num800;

array chars $ char1-char800;

do over nums;

chars=put(nums,best.);

end;

run;

Thanks,

Jag

Jag

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

06-14-2015 06:17 AM

Thanks Jag,

The code works, however, for the numbers less than 1 or including minus, it only gives me '0' or '-'

Is it possible to solve this problem?

Thanks

Noleen

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

06-14-2015 09:09 AM

Please use the put function with best32. format

Thanks,

Jag

Jag

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

06-15-2015 08:26 AM

Hi Jag,

It doesn't work with best32. format.

Char1-char800 is empty after apply best32. format in put function.

I don't know how to attach file in reply. I can send the sample data to your email if you don't mind.

Thanks a lot.

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

06-15-2015 08:44 AM

You need to show your work. AND show some sample data.

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

06-15-2015 09:23 AM

Hi

Please see the attachment for the sample data.

I used Jag's code.

data want;

set try;

array nums(*) num1-num800;

array chars(*) $ char1-char800;

do i = 1 to dim(nums);

chars(i)=put(nums(i),best.);

end;

run;

It convert the numbers less than 1 or 0 to '0' or missing.

After I change the format to best32.

data want;

set try;

array nums(*) num1-num800;

array chars(*) $ char1-char800;

do i = 1 to dim(nums);

chars(i)=put(nums(i),best32.);

end;

run;

char1-char800 return blank.

There is no error or warning.

Thanks a lot!

Solution

06-15-2015
10:00 AM

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

06-15-2015 10:00 AM

Try forcing the lengths of your variables to match the length of your format. For example, to create new variables that are 16 characters long:

array chars (800) $ 16 char1-char800;

Then convert numerics to a 16-character format:

chars(i) = put(nums(i), best16.);

You don't have to pick 16 characters, but you should match the lengths and the formats.

Good luck.

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

06-17-2015 03:49 AM

It works!!

Thanks a lot!