Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » Convert all character variables to numeric automatically

Convert all character variables to numeric automatically

BySubhro Posted onSeptember 26, 2021August 25, 2022 Last Updated:August 25, 2022
0 Comments

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

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!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #convert all character variables to numeric sas#convert multiple character variables to numeric sas#sas change all character variables to numeric
Subhro

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Facebook Twitter Linkedin

Post navigation

Previous Previous
How to get today’s date in SAS?
NextContinue
Get column value from column name

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You