How can we convert a character variable to a numeric variable using the SAS programming language? 2

How can we convert a character variable to a numeric variable using the SAS programming language?

Converting a character variable to a numeric variable in the SAS programming language is a simple process that involves using the INPUT function. This function allows you to specify the desired format for the numeric variable and then converts the character data accordingly. By using the INPUT function, you can easily manipulate and analyze the data in your SAS program, making it a useful tool for data analysis and manipulation. With the correct usage of the INPUT function, you can efficiently convert a character variable to a numeric variable in your SAS program and perform various statistical operations on the data.

SAS: Convert Character Variable to Numeric


You can use the input() function in SAS to convert a character variable to a numeric variable.

This function uses the following basic syntax:

numeric_var = input(character_var, comma9.);

The following example shows how to use this function in practice.

Related:

Example: Convert Character Variable to Numeric in SAS

Suppose we have the following dataset in SAS that shows the total sales made by some store during 10 consecutive days:

/*create dataset*/
data original_data;
    input day $ sales;
    datalines;
1 7
2 12
3 15
4 14
5 13
6 11
7 10
8 16
9 18
10 24
;
run;

/*view dataset*/
proc printdata=original_data;

We can use proc contents to view the data type of each variable in the dataset:

/*display data type for each variable*/
proc contentsdata=original_data;

We can see that day is a character variable and sales is a numeric variable.

We can use the following code to create a new dataset in which we convert the day variable from character to numeric:

/*create new dataset where 'day' is numeric*/
data new_data;
    set original_data;
    numeric_day = input(day, comma9.);
    drop day;
run;

/*view new dataset*/
proc printdata=new_data; 

Note: We used the drop function to drop the original day variable from the dataset.

/*display data type for each variable in new dataset*/
proc contentsdata=new_data;

We can see that the new variable we created, numeric_day, is a numeric variable.

Additional Resources

The following tutorials explain how to perform other common tasks in SAS:

Cite this article

stats writer (2024). How can we convert a character variable to a numeric variable using the SAS programming language?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-we-convert-a-character-variable-to-a-numeric-variable-using-the-sas-programming-language/

stats writer. "How can we convert a character variable to a numeric variable using the SAS programming language?." PSYCHOLOGICAL SCALES, 1 Jul. 2024, https://scales.arabpsychology.com/stats/how-can-we-convert-a-character-variable-to-a-numeric-variable-using-the-sas-programming-language/.

stats writer. "How can we convert a character variable to a numeric variable using the SAS programming language?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-we-convert-a-character-variable-to-a-numeric-variable-using-the-sas-programming-language/.

stats writer (2024) 'How can we convert a character variable to a numeric variable using the SAS programming language?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-we-convert-a-character-variable-to-a-numeric-variable-using-the-sas-programming-language/.

[1] stats writer, "How can we convert a character variable to a numeric variable using the SAS programming language?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, July, 2024.

stats writer. How can we convert a character variable to a numeric variable using the SAS programming language?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top