BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Noleen
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

9 REPLIES 9
Jagadishkatam
Amethyst | Level 16

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
Jagadishkatam
Amethyst | Level 16

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
Noleen
Calcite | Level 5

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

Jagadishkatam
Amethyst | Level 16

Please use the put function with best32. format

Thanks,
Jag
Noleen
Calcite | Level 5

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.

data_null__
Jade | Level 19

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

Noleen
Calcite | Level 5

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!

Astounding
PROC Star

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.


sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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