SAS: How to Convert Character to Numeric Variable

Deepanshu Bhalla Add Comment

In SAS, the INPUT function is used to convert a character variable to a numeric variable. You can specify a informat within the INPUT function to control the conversion. The informat tells SAS how to interpret the data in the character variable.

Below is the syntax of INPUT function.

new_numeric_variable = input(character_variable, comma9.);
Example: Convert Character to Numeric Variable in SAS

Here we are creating a sample dataset for demonstration. The dataset consists of a character variable with length 10. The dataset name is example and the character variable name is var1.

data example;
  input var1 $10.;
  datalines;
123
456
789
;
run;


data want;
set example;
var2 = input(var1, comma9.);
run;

To confirm conversion, we can use PROC CONTENTS to check the data type and length of variable var2.

proc contents data=want;
run;
SAS: Convert Character to Numeric Variable

As shown in the image above, var2 is a numeric variable whereas var1 is a character variable.

Note- comma9. also handles character variable having commas or dollar signs.

In order to use the same variable name after conversion, you can use DROP= option to remove the character variable. Then you can rename the new numeric variable to the old name using RENAME statement.

data want (drop = var1);
set example;
var2 = input(var1, comma9.);
rename var2=var1;
run;
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "SAS: How to Convert Character to Numeric Variable"
Next → ← Prev