In SAS, the PUT
function is used to convert a numeric variable to a character variable. You can specify a format within the PUT function to control the conversion.
Below is the syntax of PUT function.
new_character_variable = put(numeric_variable, 8.);
Let's create a sample dataset consisting of a numeric variable. The dataset name is example
and the numeric variable name is var1
.
data example; input var1; datalines; 10 20 30 40 50 ; run; data want; set example; var2 = put(var1, 8.); run;
To validate 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 character variable whereas var1 is a numeric variable.
To maintain the same variable name after converting a numeric variable to a character variable in SAS, you can use the DROP=
option to exclude the numeric variable from the dataset. Afterwards, you can proceed to rename the newly created character variable to match the original variable name using RENAME
statement.
data want (drop= var1); set example; var2 = put(var1, 8.); rename var2=var1; run;
Share Share Tweet