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!
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.
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
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,
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
Please use the put function with best32. format
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.
You need to show your work. AND show some sample data.
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!
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.
It works!!
Thanks a lot!
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.