What is the difference between PUT and INPUT in SAS? 2

What is the difference between PUT and INPUT in SAS?

PUT and INPUT are two important functions in SAS (Statistical Analysis System) used for data manipulation and processing. While both functions are used to assign values to variables, there are some key differences between them.

PUT is mainly used for converting data into a specific output format, such as changing the format of a variable from numeric to character or vice versa. It is also used to concatenate strings, perform arithmetic operations, and manipulate dates and times. PUT function does not overwrite the existing values of variables and creates a new variable with the converted data.

On the other hand, INPUT is used for converting data from a character format to a numeric format. It is also used to read data from external sources and convert it into SAS data sets. Unlike the PUT function, INPUT can overwrite the existing values of variables.

In summary, PUT and INPUT are complementary functions used for data manipulation in SAS. PUT is used for converting data into a specific format, while INPUT is used for converting data from one format to another. Understanding the differences between these two functions is crucial for efficient data processing in SAS.

SAS: The Difference Between PUT vs. INPUT


You can use the PUT and INPUT functions in SAS to convert variables to different data types.

Here is the difference between the two functions:

The PUT function takes character or numeric variables as input and always outputs character variables.

The INPUT function only takes character variables as input and can output character or numeric variables.

The following examples show two common ways to use the PUT and INPUT functions in practice.

Example 1: Using PUT to Convert Numeric Variable to Character Variable

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 and sales are both numeric variables.

We can use the PUT function to convert the day variable from numeric to character:

/*create new dataset where 'day' is character*/
data new_data;
    set original_data;
    char_day = put(day, 8.);
    drop day;
run;

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

We can use proc contents once again to check the data type of each variable in the new dataset:

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

We successfully used the PUT function to convert the day variable from numeric to a new character variable called char_day.

Example 2: Using INPUT to Convert Character Variable to Numeric Variable

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 INPUT function to 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; 

We can use proc contents once again to check the data type of each variable in the new dataset:

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

We successfully used the INPUT  function to convert the day variable from a character variable to a new numeric variable called numeric_day.

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

Cite this article

stats writer (2024). What is the difference between PUT and INPUT in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/what-is-the-difference-between-put-and-input-in-sas/

stats writer. "What is the difference between PUT and INPUT in SAS?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/what-is-the-difference-between-put-and-input-in-sas/.

stats writer. "What is the difference between PUT and INPUT in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/what-is-the-difference-between-put-and-input-in-sas/.

stats writer (2024) 'What is the difference between PUT and INPUT in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/what-is-the-difference-between-put-and-input-in-sas/.

[1] stats writer, "What is the difference between PUT and INPUT in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. What is the difference between PUT and INPUT in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

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