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 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.

Additional Resources

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

SAS: How to Convert Character Variable to Numeric
SAS: How to Convert Numeric Variable to Character
SAS: How to Convert Character Variable to Date
SAS: How to Convert Numeric Variable to Date

2 Replies to “SAS: The Difference Between PUT vs. INPUT”

  1. Thank you somuch for info.
    query:
    input fucntion numeric to numeric conversion and
    put function character to character conversion examples pls

    1. Hi rajesh…In **SAS**, the `INPUT` function is used to convert **character** data into **numeric** values, while the `PUT` function is used to convert **numeric** data into **character** values.

      ### **1. INPUT Function: Character to Numeric Conversion**
      The `INPUT` function reads a character variable and converts it into a numeric variable using an appropriate numeric format.

      #### **Example: Converting Character to Numeric**
      “`sas
      data input_example;
      char_num = “12345”; /* Character variable */
      num_var = input(char_num, 8.); /* Converting character to numeric */
      run;
      “`
      – Here, `”12345″` is a character variable.
      – `INPUT(char_num, 8.)` converts it into a numeric variable (`num_var`).
      – The format `8.` means the number can have up to 8 digits.

      ### **2. PUT Function: Numeric to Character Conversion**
      The `PUT` function is used to convert a numeric variable into a character variable using an appropriate format.

      #### **Example: Converting Numeric to Character**
      “`sas
      data put_example;
      num_var = 12345; /* Numeric variable */
      char_var = put(num_var, 8.); /* Converting numeric to character */
      run;
      “`
      – Here, `12345` is a numeric value.
      – `PUT(num_var, 8.)` converts it into a character string (`char_var`).

      ### **Additional Use Cases**
      #### **1. INPUT Function (Numeric-to-Numeric Conversion)**
      – If a number is stored as a character but contains decimal places, we can convert it properly.
      “`sas
      data input_numeric;
      char_num = “123.45”; /* Character representation of a number */
      num_var = input(char_num, best32.); /* Converting character to numeric */
      run;
      “`
      – `best32.` format ensures that SAS correctly interprets the number.

      #### **2. PUT Function (Character-to-Character Conversion)**
      – If we want to format a character variable in a specific way.
      “`sas
      data put_character;
      char_date = “20250225”; /* Character representation of a date */
      formatted_date = put(input(char_date, yymmdd8.), date9.); /* Convert to proper date format */
      run;
      “`
      – `INPUT(char_date, yymmdd8.)` first converts the character string `”20250225″` into a SAS date value.
      – `PUT(…, date9.)` formats the date as `25FEB2025`.

Leave a Reply

Your email address will not be published. Required fields are marked *