How to convert large amount of numeric variables to character variables?

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

How to convert large amount of numeric variables to character variables?

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 Smiley Sad

Many thanks!


Accepted Solutions
Solution
‎06-15-2015 10:00 AM
Super User
Posts: 5,362

Re: How to convert large amount of numeric variables to character variables?

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.


View solution in original post


All Replies
Trusted Advisor
Posts: 1,137

Re: How to convert large amount of numeric variables to character variables?

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
Trusted Advisor
Posts: 1,137

Re: How to convert large amount of numeric variables to character variables?

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
Occasional Contributor
Posts: 16

Re: How to convert large amount of numeric variables to character variables?

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

Trusted Advisor
Posts: 1,137

Re: How to convert large amount of numeric variables to character variables?

Please use the put function with best32. format

Thanks,
Jag
Occasional Contributor
Posts: 16

Re: How to convert large amount of numeric variables to character variables?

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.

Respected Advisor
Posts: 3,786

Re: How to convert large amount of numeric variables to character variables?

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

Occasional Contributor
Posts: 16

Re: How to convert large amount of numeric variables to character variables?

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
Super User
Posts: 5,362

Re: How to convert large amount of numeric variables to character variables?

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.


Occasional Contributor
Posts: 16

Re: How to convert large amount of numeric variables to character variables?

It works!!

Thanks a lot!

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 659 views
  • 9 likes
  • 4 in conversation