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.);
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;
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;
Share Share Tweet