DATA Step, Macro, Functions and more

Convert all numeric variables to Character type

Super Contributor
Posts: 647

Convert all numeric variables to Character type

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?
Posts: 8,743

Re: Convert all numeric variables to Character type

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:
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);

ods listing;
proc contents data=tempclass;

proc print data=tempclass;

/* 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;


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?

Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation