SAS: How to Convert Numeric to Character Variable

Deepanshu Bhalla Add Comment

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.);
Example: Convert Numeric to Character Variable in SAS

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;
Convert Numeric to Character Variable in SAS

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;
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 Numeric to Character Variable"
Next → ← Prev