Converting between character and numeric using INPUT and PUT
Why type conversion is necessary
In SAS every variable is stored as either a character string or a number — there is no automatic coercion between the two types
Attempting to assign a character value to a numeric variable or vice versa causes SAS to convert automatically but this implicit conversion is unreliable, generates warnings, and should be avoided
The correct approach is to use the `INPUT` function to convert character text to a numeric value, and the `PUT` function to convert a numeric value to character text
These two functions are among the most frequently used in clinical and production SAS programming
Create sample data with type conversion challenges
We create a dataset where numeric values have been read in as character strings (a common scenario when importing from flat files or spreadsheets)
We also create a dataset where we need to convert numeric codes to formatted character labels
SAS Log
In `raw_import`, `age_char`, `weight_char`, and `visit_date_char` are all character variables even though they represent numeric values and a date
In `coded_data`, `sex_code` and `result_code` are numeric and will be converted to character labels using PUT
Dataset View
The INPUT function - character to numeric
INPUT function syntax
The INPUT function has the form: `INPUT(source, informat)`
`source` is the character variable or expression to convert
`informat` tells SAS how to interpret the text — for plain numbers use `best32.` or a specific width such as `8.`, and for dates use an appropriate date informat such as `date9.` or `ddmmyy10.`
The result is a numeric value
If the text cannot be converted using the specified informat SAS sets the result to missing and optionally issues a NOTE or WARNING
Convert character numbers to numeric
Here we convert `age_char` and `weight_char` from character to numeric in a DATA step
We also handle the case where the weight value is a dot representing missing — `INPUT` correctly returns a numeric missing for this
SAS Log
Inspect `converted` and confirm that `age` and `weight` are numeric variables
Subject 004 should have a missing numeric value for `weight` because the source was a dot
You can verify variable types by running `proc contents data=converted;` and checking the TYPE column
Dataset View
Convert a character date to a SAS date value
Date values stored as text such as `15JAN2024` must be read with a date informat that matches the text format
`date9.` reads dates in DDMonYYYY format such as `15JAN2024`
The result is a numeric SAS date value (the number of days since 1 January 1960)
Always assign a DATE format to the resulting variable so it displays as a readable date rather than a raw number
SAS Log
Inspect `with_dates` and confirm that `visit_date` is a numeric variable with a DATE9 format applied
The display should show the original date strings back in readable form because the format and informat match
Now that `visit_date` is a true SAS date it supports arithmetic such as computing days between dates
Dataset View
The PUT function - numeric to character
PUT function syntax
The PUT function has the form: `PUT(source, format)`
`source` is the numeric variable or expression to convert
`format` controls how the number is rendered as text — for plain numbers use `best32.` or a specific width such as `8.`, and for dates use a date display format such as `date9.`
The result is a character string
Unlike INPUT, PUT always succeeds — it simply renders the numeric value as text using the format you specify
Convert a numeric code to a character label
A common use of PUT is to convert numeric flag or code variables to character labels for reporting or for creating a new flag variable
Here we apply a user-defined format to decode numeric sex codes and result codes into readable text
SAS Log
Inspect `decoded` and confirm that the `sex_label` and `result_label` columns contain character strings, not numeric values
Subject 005 has a missing `result_code` — PUT with a user-defined format applied to numeric missing returns a blank character string by default
Dataset View
Convert a SAS date to a character string
Sometimes a SAS date needs to be stored as a character string — for example when building a file name suffix or when concatenating into a title or label
PUT with a date display format converts the numeric date to text in the chosen layout
SAS Log
`date_str_9` stores the date in DDMonYYYY format such as `15JAN2024`
`date_str_yymd` stores the date in YYYY-MM-DD format such as `2024-01-15`
Both variables are character strings, not dates — they cannot be used in date arithmetic but are useful for display, file naming, or text concatenation
Dataset View
Convert numeric to a padded character string using the Z format
A practical use of PUT is creating zero-padded ID strings from numeric values
For example, a subject ID stored as a number such as 1 can be padded to `001` using the Z format
SAS Log
The `z3.` format produces a 3-character zero-padded result so 1 becomes `001` and 10 becomes `010`
Inspect `padded_ids` and confirm the character lengths and zero-padding are correct
Dataset View
Key points to remember
`INPUT(source, informat)` converts character to numeric — always specify an informat that matches the text format of the source
`PUT(source, format)` converts numeric to character — always specify a format that produces the text representation you need
For date conversion: use `INPUT` with a date informat to read text dates into SAS date values, and `PUT` with a date format to display SAS date values as text
After INPUT the result is a numeric variable — assign a FORMAT statement if you want it to display as a date or formatted number
After PUT the result is a character variable — it supports string operations but not date arithmetic
Avoid relying on SAS automatic conversion which generates WARNING messages and may produce unexpected results in edge cases