PUT vs. INPUT in SAS: Type Conversions & Mental Models
Master explicit variable conversions between Character and Numeric types and prevent silent data corruption.
In Base SAS, INPUT() and PUT() are explicit type conversion functions. The INPUT() function converts a character string into a numeric value (or formatted value) using an INFORMAT, reading raw text 'IN' to internal SAS binary storage. The PUT() function writes an internal numeric or character value 'OUT' as a character string using a FORMAT. Unlike implicit casting, explicit conversions guarantee reproducible data pipelines and prevent silent missing values.
Why This Concept Matters in Real-World Practice #
Relying on implicit type conversion in SAS triggers warning notes in the execution log ('NOTE: Character values have been converted to numeric...'). If raw data contains unexpected characters (like '$', commas, or 'N/A'), implicit conversions silently fail and assign missing values (.), corrupting downstream statistical calculations. Furthermore, because a variable's data type is immutable once defined in the Program Data Vector (PDV), programmers must master the 'Rename and Drop' pattern to safely convert variable types in place.
Standard Syntax, Derivation & Framework Pattern #
/* ==============================================================
EXPLICIT TYPE CONVERSION PATTERNS: INPUT() vs. PUT()
============================================================== */
/* Step 1: Character to Numeric using INPUT() with INFORMATS */
DATA work.convert_to_numeric;
Raw_Income = "$125,400.50";
Raw_Date = "15MAY2026";
/* INPUT: Character source + Informat -> Numeric Target */
Income_Num = INPUT(Raw_Income, COMMA12.2);
Date_Num = INPUT(Raw_Date, DATE9.);
FORMAT Date_Num YYMMDD10. Income_Num DOLLAR12.2;
RUN;
/* Step 2: Numeric to Character using PUT() with FORMATS */
DATA work.convert_to_char;
Date_Num = 24241; /* May 15, 2026 */
Cost_Num = 8450.75;
/* PUT: Numeric source + Format -> ALWAYS Character Target */
Date_ISO = PUT(Date_Num, YYMMDD10.); /* "2026-05-15" */
Cost_Label = STRIP(PUT(Cost_Num, DOLLAR12.2)); /* "$8,450.75" */
RUN;
/* Step 3: In-Place Variable Type Replacement (The Rename & Drop Dance) */
/* A variable cannot change type in the PDV in place! */
DATA work.clean_inventory (DROP=temp_sku);
SET raw.inventory (RENAME=(SKU=temp_sku));
/* Safely convert SKU from Character to Numeric with same variable name */
SKU = INPUT(temp_sku, 8.);
RUN;
Core Rules #
- The Golden Mnemonic: INPUT reads raw text IN to SAS (produces Numbers); PUT writes formatted values OUT (produces Strings).
- INPUT() requires an INFORMAT (e.g. COMMA10., DATE9.); PUT() requires a FORMAT (e.g. DOLLAR12.2, YYMMDD10.).
- The output of PUT() is ALWAYS a character string (100% of the time), regardless of the source variable's type.
- Never rely on implicit conversion: SAS uses BEST12. for numeric-to-char conversion, adding unwanted leading blanks that break text concatenations.
- PDV Immutability: A variable's type cannot be altered within a single DATA step without using the SET (RENAME=(var=temp)) and DROP=temp pattern.
How explicit conversions prevent silent log notes, PDV compilation errors, and leading whitespace concatenation bugs.
Frequently Asked Questions (FAQ) #
How can I remember whether to use PUT or INPUT?
Use the direction of data flow: INPUT takes raw text and reads it IN to internal SAS numbers (Character -> Numeric). PUT takes internal data and PUTS it OUT to display as text (Numeric -> Character). PUT always outputs a character string.
What causes the error 'Variable X has been defined as both character and numeric'?
In the SAS Program Data Vector (PDV), once a variable name is declared with a specific data type (either from an incoming dataset or preceding statement), its type is permanently locked. Writing 'X = INPUT(X, 8.);' tries to redefine X as numeric while it is already defined as character. To solve this, rename X in the SET statement and drop the temporary variable.
Why does concatenating a number with a string add leading spaces?
When you write 'ID_Code = "SUBJ_" || Num_ID;', SAS performs an implicit conversion using the default BEST12. format. BEST12. right-aligns numbers across a 12-character field, resulting in 9 to 11 leading spaces. To fix this, always use explicit conversion with STRIP: '"SUBJ_" || STRIP(PUT(Num_ID, 8.))'.
Where can I read the full technical breakdown and interview traps on this topic?
Read our comprehensive companion guide: PUT vs. INPUT in SAS: The Mental Model That Prevents Silent Data Corruption, detailing PDV memory allocation, date calculations, and common certification exam traps.
Want complete video breakdowns, code files & exercises?
Explore comprehensive step-by-step masterclass training in our authoritative SAS Certification Course & Programming Training or test your code mechanics on the SAS Base Exam Simulator (325 Questions).