Convert all character variables to numeric automatically

Convert all character variables to numeric automatically

Are you looking to convert all character variables to numeric variables?

A character variable can store numbers, but analyses require numeric variables. This example shows how to convert all character variables to numeric.

Sample dataset

data test; 
input name $ marks1 $ marks2 $ marks3 $; 
datalines; 
Alfred 50 61 91 
Alice 35 82 82 
Barbara 75 63 73 
; 
run;
Convert all character variables to numeric

We will get the count of all character variables in the dataset.

For more information, see our post on 8 Ways to count the number of observations in a SAS dataset and pass it into a macro variable.

proc sql noprint;
select count(name) into :cnt from dictionary.columns where 
libname=upcase("WORK") and memname=upcase("CLASS") and type="char";
quit;
%put &cnt.;

Output:

4

Then, we will determine the list of variables that have numeric values.

This is done using the INPUT function to read the value of the character variable vars[m] with the numeric informat 3.

The result is a numeric value or, if the value of vars[m] is non-numeric, a missing value is generated, and the flag variable is set to 1. (The ?? after the comma prevents printing an error message in the log and setting the automatic variable _ERROR_ to 1 if the value of vars[m] is non-numeric.)

Read How to check if a string is numeric in SAS?

data _null_;
set class end=lastobs;
array vars [*] _character_;
array flag{&cnt} _temporary_;
do m = 1 TO dim(vars);
flag[m]=ifn ((input(vars[m], ?? 3.) eq .), 0, 1); 
END;
if lastobs then
do;
length varlist $ 32767;
do j=1 to &cnt;
if flag{j} then
varlist=catx(' ', varlist, vname(vars{j}));
end;
call symputx('varlist', varlist);
end;
run;

%put &varlist;
%let nvars=%sysfunc(countw(&varlist));
%put &nvars.

The SET statement reads observations from the SAS data set Class. The END= option creates a temporary variable named LASTOBS that is initialized to 0. LASTOBS equals 1 when SET reads the last observation in the SAS data set class.

The first array statement creates an array named vars for all the character variables. The second array statement creates a temporary array named flag to store the result of the flag variable.

Output:

marks1 marks2 marks3
3

The next step is used to convert a character variable to a numeric variable, drop and rename macro.

Read: Variable conversions in SAS

data class2;
set class;
array charx{&nvars} &varlist;
array x{&nvars};

do i=1 to &nvars;
x{i}=input(charx{i}, 3.);
end;

do i=1 to &nvars;
drop &varlist i;
%renamer;
end;
run;

Rename Macro

%macro renamer;
%do i=1 %to &nvars;
rename x&i=%scan(&varlist, &i);
%end;
%mend renamer;
Convert all character variables to numeric

Reference: Convert all character variables to numeric and use the same variable names in the output data set

You can download this entire code from here.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro

Subhro provides valuable and informative content on SAS, offering a comprehensive understanding of SAS concepts. We have been creating SAS tutorials since 2019, and 9to5sas has become one of the leading free SAS resources available on the internet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.