What’s the difference between PUT and INPUT in SAS?

PUT is a SAS statement that writes a character string, a numeric value, or the formatted value of a variable to the SAS log or an external file. INPUT is a SAS statement that reads raw data values from an external file or an internal line of data and stores them in SAS variables.


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 print data=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 contents data=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 print data=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 contents data=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 print data=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 contents data=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 print data=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 contents data=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:

x