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
SAS Super FREQ
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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1 reply
  • 1206 views
  • 0 likes
  • 2 in conversation