BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
How to convert all numeric variables in a data set to character type?Or how to convert all variables in the dataset to character type irrespective of whether or not they are numeric or character?
1 REPLY 1
Cynthia_sas
Diamond | Level 26
Hi:
Unless you wrote a macro program, you can't -automatically- convert your numeric variables to character. For one thing, you would not want to allow SAS to do an automatic conversion of numeric to character -- you'd want to explicitly control the conversion using the PUT function -- which means you'd at least have to know enough about the data to code and use the right format for the PUT function.

Once the SAS descriptor portion of the data set has defined variables as character or numeric, you can't respecify their type without doing some shuffling and renaming. For example:
[pre]
data tempclass;
set sashelp.class;
drop nage nheight nweight;
set sashelp.class(rename=(age=nage height=nheight weight=nweight));
age = put(nage,2.0);
height = put(nheight,5.1);
weight = put(nweight,5.1);
run;

ods listing;
proc contents data=tempclass;
run;

proc print data=tempclass;
run;

/* carefully examine the output from the above steps and do NOT do this PROC SORT for "real"
or you will overwrite sashelp.class */
/*
** proc sort data=tempclass out=sashelp.class;
** by name;
** run;
*/

[/pre]

However, I really, really can't understand why you would want to convert all your numeric variables to character variables. Many SAS procedures need for variables to be numeric in order to perform analysis.

You can't get the MEAN or the MAX of a character value from PROC MEANS, for example. Why do you need to do this??? Do you just need to change from right justify to left justify the values on the report?

cynthia

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 1564 views
  • 0 likes
  • 2 in conversation