Understanding SAS Data Conversion: A Detailed Comparison of the PUT and INPUT Functions


In the demanding world of data science and statistical computing, particularly within SAS programming, the need to accurately manage and transform data types is fundamental to producing valid results. Data conversion—moving data between its internal numeric representation and its external character string format—is a core requirement for everything from data cleaning to advanced reporting. This critical transformation is managed by two essential, yet opposing, tools: the PUT function and the INPUT function. These functions allow developers to seamlessly change variables from character variables to numeric variables, and vice versa. While both are indispensable, understanding their inverse relationship—one for output, one for input—is paramount for writing efficient, error-free, and robust SAS code.

Defining the Direction: PUT vs. INPUT Functions

The fundamental distinction between the PUT function and the INPUT function hinges entirely on the direction of data conversion relative to SAS’s internal storage structure. They operate as mirror images: one takes data stored internally and prepares it for external display (PUT), while the other takes external, raw text and prepares it for internal computation (INPUT). Recognizing this primary flow—from internal to external or external to internal—is the foundational concept required for advanced data manipulation in SAS environments.

The PUT function is best conceptualized as a dedicated output or formatting utility. Its purpose is to convert any variable—be it numeric (like a date value) or character (like a name)—into a newly formatted character string. This means that the PUT function always yields a character variable as its output. The function works by applying a specified format to the source data, allowing the user to control the textual appearance of the internal value, such as converting the numeric SAS date 23000 into the visible string ’01JAN2023′. This mechanism is vital for tasks requiring standardized text output, such as creating unique composite identifiers, exporting fixed-width files, or generating custom report labels.

In direct contrast, the INPUT function serves as an internal parsing and interpretation tool. It accepts only a character string as its source and attempts to read or decode that string based on a set of instructions provided by an informat. The resulting variable can be either a character or a numeric variable, depending on the informat used. The primary application of INPUT is importing raw, text-based data—which often contains numbers formatted with non-numeric characters (like ‘$1,000’) or dates stored as text—and transforming it into a clean, usable internal data type suitable for mathematical operations and analysis within the SAS system.

The PUT Function: Transforming Internal Data for Output

The PUT function stands as the authoritative method within SAS for converting existing variables—whether they are numeric variables or character variables—into a newly formatted character variable. Its straightforward syntax, PUT(source, format), requires two components: the source variable containing the data to be converted, and a specific SAS format that strictly governs how the final character representation will appear. This process is essential for controlling the output’s precision, padding, and overall structure.

The primary use cases for the PUT function involve situations where internal numeric data needs to be integrated into text streams or outputted to external systems that mandate text-only fields. For example, a numeric value representing an employee ID (e.g., 4567) might need conversion to a string like ‘EMP-04567’ to meet reporting standards or concatenation requirements. By applying the appropriate format, the PUT function guarantees consistent data representation, making it indispensable for tasks such as generating standardized reports, creating structured file exports, or building complex, descriptive labels within the SAS environment.

Selecting the correct format is critical for successful execution. If the source variable holds numeric data, you must pair it with a numeric format, such as BEST12. for general numbers or DDMMYY10. for date values. If the source is already a character variable, a character format like $CHAR. can be used to manage length and padding. It is a non-negotiable rule that the result of the PUT function, irrespective of the source variable type, will always be a character string, ready for textual output.

The INPUT Function: Interpreting Raw Text into Structured Data

The INPUT function is specifically engineered to handle the reverse operation of PUT: converting raw character data into a usable internal data type, which may be either numeric or character. Its structure is defined as INPUT(source, informat). In this syntax, the source must be a character variable containing the textual data, and the informat provides the detailed instructions—a set of rules—that SAS uses to read, interpret, and translate the text string into the desired format.

The most frequent use case for INPUT is during the data loading process, especially when importing files where numerical measures (like currency, percentages, or dates) are stored as text strings. For analytical purposes, data such as ’10-JAN-2024′ or ‘99.9%’ must be transformed into true numeric variables before they can be utilized in arithmetic calculations, statistical procedures, or accurate sorting. The designated SAS informat is the mechanism that instructs the system how to strip away extraneous characters (like commas or dollar signs) or correctly parse textual date representations.

Careful selection of the informat is absolutely vital for ensuring data quality. If the chosen informat does not precisely match the structure of the incoming character data—for instance, trying to read alphabetic characters using a numeric informat—SAS will be unable to complete the conversion successfully. In such cases of conversion failure, SAS automatically assigns a missing value to the new variable and records a warning message in the log. Proficient use of the INPUT function involves rigorous review of the SAS log to proactively identify and fix these data quality issues before analysis begins.

Practical Demonstration 1: Numeric-to-Character Conversion Using PUT

Consider a standard requirement in SAS reporting where a key identifier, such as ‘day,’ is stored internally as a numeric variable. If we need to concatenate this numeric day with a prefix (e.g., “D”) or export it to a system that mandates all non-analytical fields be textual, we must convert ‘day’ into a character string using the PUT function.

We initiate the process by creating a foundational dataset, original_data, which captures daily sales figures. Note that in this initial structure, both the day and sales variables are implicitly defined as numeric based on the input values:

/*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;

To formally verify the initial variable attributes, we execute the PROC CONTENTS procedure. This essential utility retrieves the dataset’s metadata, confirming that both day and sales are currently stored internally as data types suitable for mathematical operations:

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

The contents output confirms that both variables are numeric. We now perform the conversion within a DATA step. We use the statement char_day = put(day, 8.);. This command executes the PUT function, taking the numeric value of day and applying the standard numeric format 8. to create the new variable, char_day. The resulting variable is a character variable with a defined maximum length, ready for text manipulation.

/*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; 

To finalize the validation of this transformation, we execute PROC CONTENTS on the newly created new_data. This step provides conclusive evidence that char_day is now correctly defined as a character variable, affirming the successful application of the PUT function for numeric-to-text conversion.

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

Practical Demonstration 2: Character-to-Numeric Conversion Using INPUT

The inverse operation involves taking a character variable that holds numerical text and converting it into a true numeric variable. This character-to-numeric transformation is essential whenever data is imported as text but needs to support calculations, statistical analysis, or mathematically correct sorting. For this example, we re-create the original_data dataset, but explicitly define ‘day’ as a character variable during the data input phase using the trailing dollar sign ($) in the INPUT statement.

/*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 utilize PROC CONTENTS once more to confirm that the day variable is now officially recognized by SAS as character data:

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

With the character status confirmed, we move to the DATA step to apply the INPUT function. The key statement here is numeric_day = input(day, comma9.);. This command instructs SAS to read the character data in day and convert it into the new numeric variable numeric_day. We use the comma9. SAS informat, which is flexible enough to handle standard numerical strings, ensuring the textual representation is accurately translated into its internal numerical value.

/*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; 

The final step involves executing PROC CONTENTS on the new_data dataset one last time. This confirms that the new variable, numeric_day, is now correctly stored as a numeric variable, validating that the INPUT function successfully interpreted the character text and converted it into a format suitable for computation.

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

Best Practices for Robust and Error-Free SAS Data Conversion

Maintaining rigorous data type integrity is the bedrock of reliable analytical work in the SAS environment. Given the potential for subtle errors during character-to-numeric or numeric-to-character transfers, developers must adopt robust practices when implementing the PUT and INPUT functions. Adherence to the following guidelines ensures data accuracy, traceability, and minimizes the risk of conversion failure, leading to cleaner code and more trustworthy analysis results.

  • Always Verify with PROC CONTENTS: The most crucial safeguard is the repeated use of PROC CONTENTS. Run this procedure both immediately before and immediately after any major conversion step. This confirms the variable’s initial state and verifies that the output variable truly possesses the anticipated data type (character or numeric), thereby preventing downstream errors caused by mistaken assumptions about variable structure.
  • Ensure Exact Format/Informat Matching: The effectiveness of these conversion functions relies entirely on the precision of the specified format (for PUT) or informat (for INPUT). Carefully match the width, decimal placement, and specific handling of special characters (e.g., ensuring COMMA. is used if the source string contains thousand separators) to the exact structure of the data being converted.
  • Explicitly Handle Missing Value Transitions: Be aware of the difference in how missing values are represented. When converting numeric to character using PUT, the numeric missing value (.) becomes a blank character string. Conversely, when using INPUT to convert a blank character string (' ') to numeric, it automatically results in a numeric missing value (.). Understanding this transition prevents misinterpretation during data export or import.
  • Preserve Original Data Integrity: It is strongly recommended to always create a new variable for the converted data (e.g., numeric_id from char_id). Overwriting the source variable complicates debugging and traceability, making it difficult to verify the original data structure if issues arise later in the processing pipeline.

Summary: Mastering the Data Conversion Lifecycle

The PUT and INPUT functions are indispensable tools, forming the core conversion mechanism in SAS programming. The rule for distinguishing between them is simple: if you are moving data from internal storage to an external representation (text/output), you must use the PUT function along with a specific display format. Conversely, if you are moving data from an external representation (raw character text) to internal storage, you must use the INPUT function, relying on an appropriate reading informat to achieve the correct data type.

By mastering this directional relationship and paying close attention to the syntax and required format/informat specifications, SAS professionals can guarantee data integrity, efficiently handle complex data inputs, and build analytical workflows that are both flexible and highly reliable, regardless of the variety of data sources encountered.

Additional Resources for Advanced SAS Techniques

To further enhance your SAS programming skills and explore more advanced data manipulation techniques, consider reviewing the following tutorials. These resources cover various common tasks that can help optimize your data processing and analytical endeavors.

Cite this article

Mohammed looti (2025). Understanding SAS Data Conversion: A Detailed Comparison of the PUT and INPUT Functions. PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/sas-the-difference-between-put-vs-input/

Mohammed looti. "Understanding SAS Data Conversion: A Detailed Comparison of the PUT and INPUT Functions." PSYCHOLOGICAL STATISTICS, 15 Nov. 2025, https://statistics.arabpsychology.com/sas-the-difference-between-put-vs-input/.

Mohammed looti. "Understanding SAS Data Conversion: A Detailed Comparison of the PUT and INPUT Functions." PSYCHOLOGICAL STATISTICS, 2025. https://statistics.arabpsychology.com/sas-the-difference-between-put-vs-input/.

Mohammed looti (2025) 'Understanding SAS Data Conversion: A Detailed Comparison of the PUT and INPUT Functions', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/sas-the-difference-between-put-vs-input/.

[1] Mohammed looti, "Understanding SAS Data Conversion: A Detailed Comparison of the PUT and INPUT Functions," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, November, 2025.

Mohammed looti. Understanding SAS Data Conversion: A Detailed Comparison of the PUT and INPUT Functions. PSYCHOLOGICAL STATISTICS. 2025;vol(issue):pages.

Download Post (.PDF)
Scroll to Top