5.5 Explicit & Implicit Data Type Conversions (INPUT & PUT)
Key Takeaways
- The INPUT function converts character values to numeric (or character) values using an INFORMAT, always returning a data type matching the informat.
- The PUT function converts numeric (or character) values to character values using a FORMAT, always returning a character string.
- Assigning a numeric result to an existing character variable is not an error: SAS converts it back to character with the BESTw. format (w = the variable's length) and writes a 'Numeric values have been converted to character values' note.
- Implicit character-to-numeric conversion occurs when character variables are used in arithmetic operations, producing a note in the SAS log.
- If implicit character-to-numeric conversion fails due to invalid characters, SAS assigns a missing value (.), sets _ERROR_ = 1, and prints an invalid data note to the log.
5.5 Explicit & Implicit Data Type Conversions (INPUT & PUT)
SAS supports exactly two data types: numeric and character. Frequently, incoming datasets contain numbers stored in character variables (e.g., "$1,250.50" or "20260101") or numeric values that must be formatted as character strings for concatenation or reporting. Converting data between character and numeric types—both explicitly and implicitly—is one of the most frequently examined domains on the SAS Certified Specialist Base Programming exam.
1. Explicit Character-to-Numeric Conversion: The INPUT Function
The INPUT function converts character values to numeric or character values based on a specified INFORMAT.
source_variable: The character variable or string to be converted.informat.: The SAS informat that instructs SAS how to read and interpret the source string.
data work.convert_char_to_num;
char_price = "$1,250.75";
char_date = "10/31/2026";
/* Convert formatted character currency to numeric */
num_price = input(char_price, dollar10.2);
/* Convert character date string to SAS date integer */
num_date = input(char_date, mmddyy10.);
format num_date date9. num_price dollar10.2;
run;
[!IMPORTANT] Memory Rule: The
INPUTfunction requires an INFORMAT (with a dot). The data type oftarget_variableis determined by the informat used. If the informat is numeric (such as8.orCOMMA9.),target_variablebecomes numeric.
2. Explicit Numeric-to-Character Conversion: The PUT Function
The PUT function converts numeric or character values into character values formatted according to a specified FORMAT.
source_variable: The numeric variable or expression to be converted.format.: The SAS format that instructs SAS how to write out the character string.
data work.convert_num_to_char;
num_code = 4096;
sas_date = 24107; /* Represents 01JAN2026 */
/* Convert numeric integer to 6-character string padded with leading zeros */
char_code = put(num_code, z6.); /* Result: '004096' */
/* Convert SAS date integer to character string formatted as YYYY-MM-DD */
char_date = put(sas_date, yymmdd10.); /* Result: '2026-01-01' */
run;
[!IMPORTANT] The
PUTfunction always returns a character string. Therefore,target_variableis always created as a character variable.
3. Variable Re-assignment Rules & Type Conflicts
In a SAS DATA step, a variable's data type is established upon its first reference during compilation and cannot be changed during execution. A widespread misconception is that re-assigning a converted value to the same variable name raises an error. It does not — SAS silently converts the value back, which is more dangerous than an error because the program keeps running with corrupted data.
/* WRONG - but NOT an error. SAS silently converts the number back to text! */
data work.error_example;
salary = "50000"; /* SALARY is CHARACTER, length 5 */
salary = input(salary, 8.); /* INPUT returns numeric 50000 ... */
run; /* ... then SAS converts it back to */
/* character using BEST5. */
SAS Log Output:
NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column).
SALARY remains a character variable. Per the SAS 9.4 language reference, when a numeric result is assigned to a character variable SAS applies the BESTw. format, where w is the length of the character variable (maximum 32). Because SALARY is 5 bytes, BEST5. reproduces '50000'. Had SALARY been declared $3, the number would not fit and SAS would fill the variable with asterisks ('***') and note invalid character data.
Correct Pattern for In-Place Conversion:
To replace a character variable with a numeric variable of the same name, use dataset options (RENAME= and DROP=) during compilation:
data work.correct_reassign (drop=char_salary);
set work.raw_data (rename=(salary=char_salary));
salary = input(char_salary, 8.); /* New numeric variable SALARY */
run;
4. Implicit (Automatic) Data Type Conversions
When a variable of one data type is used in a context that requires the other data type, SAS attempts an implicit type conversion automatically.
Character-to-Numeric Implicit Conversion
Implicit character-to-numeric conversion occurs when a character variable is used in a numeric context, such as:
- Arithmetic operators (
+,-,*,/) - Logical comparison with numeric constants
- Assignment to a numeric variable
data work.implicit_num;
char_val = "100";
total = char_val + 50; /* Implicitly converts char_val to numeric 100 */
run;
SAS Log Output:
NOTE: Character values have been converted to numeric values at the places given by: Line 3 Column 13.
What Happens If Implicit Conversion Fails?
If char_val contains non-numeric characters (e.g., "100ABC" or "$50"), SAS cannot perform the implicit conversion:
- SAS sets the target variable to missing (
.). - SAS sets the automatic variable
_ERROR_ = 1. - SAS writes a note to the log:
NOTE: Invalid numeric data, '100ABC' , at line 3 column 13.
Numeric-to-Character Implicit Conversion
Implicit numeric-to-character conversion occurs when a numeric variable appears in a character context, such as:
- Character functions (e.g.,
SUBSTR,SCAN,CATX) - Concatenation operator (
||) - Assignment to a character variable
When converting numeric to character implicitly, SAS applies the BEST12. format, right-aligning the result in a 12-byte string.
data work.implicit_char;
num_id = 789;
/* Implicitly converts num_id using BEST12. format */
combo = "ID-" || num_id;
run;
[!CAUTION] Because SAS uses
BEST12.for implicit numeric-to-character conversion, the resulting string contains leading blanks (9 spaces followed by789). Thus,combobecomes"ID- 789"! Perform an explicit conversion and strip the padding instead:combo = "ID-" || strip(put(num_id, best12.));or simplycombo = cats("ID-", num_id);. Note thatstrip()must wrap the wholePUT()call —put(num_id, strip(best.))is invalid syntax, because the second argument ofPUTmust be a format, never a function call.
5. Summary Matrix: INPUT vs. PUT
| Feature | INPUT Function | PUT Function |
|---|---|---|
| Primary Purpose | Converts Character $\rightarrow$ Numeric | Converts Numeric $\rightarrow$ Character |
| Argument Required | INFORMAT (e.g., comma9., mmddyy10.) | FORMAT (e.g., z5., dollar10.2) |
| Return Type | Defined by informat (usually numeric) | Always CHARACTER |
| Best Used For | Reading raw strings into usable numbers | Formatting values for output/strings |
| Handling Blanks | Reads formatted input strings | Right-aligns numbers via format |
Which SAS function and argument syntax should be used to convert a character variable named CHAR_CODE containing value "00458" into a numeric variable NUM_CODE?
Consider the following DATA step: data work.implicit; val = "500 USD"; total = val + 100; run; What happens during execution of this DATA step?
A SAS programmer attempts to execute the following code block: data work.convert; emp_id = "00984"; emp_id = input(emp_id, 5.); run; What is the outcome of compiling and running this code?
When SAS performs an implicit numeric-to-character conversion, which format does it automatically apply to format the numeric value?