Written and maintained by CASRAI Editorial Board
Published
On this page
To convert a character variable to numeric in SAS, use the INPUT function with an informat that matches how the text is written: new_var = input(char_var, 8.);. SAS does not let a variable change type, so INPUT always writes to a new variable; if you need the original name back, drop the character variable and rename the new one. Check the result with PROC CONTENTS, and expect a missing value and a log note wherever the text does not match the informat.
What the SAS INPUT function is used for in research
SAS has two basic data types, numeric and character. Research data often arrives in the wrong one: a spreadsheet export where income was stored as text, a survey file where a code such as a ZIP code was read as a number, or a date column that came through as the string 12JUL2016. Arithmetic, means, regression and date functions need numeric values, so a character column has to be converted before it can be analysed. The University of Wisconsin Social Science Computing Cooperative (SSCC) gives the typical case: a character string such as 50000 that represents an annual income that you want to use in a calculation.
The SAS documentation describes the INPUT function as converting a SAS character expression using a specified informat; the informat decides whether the result is numeric or character. That is why the informat you choose matters more than the function itself. The same call, input(x, 8.), reads plain digits, whereas input(x, comma10.) also reads thousands separators. If the first argument is a literal string rather than a variable, it must be in quotation marks.
Many analysts reach for INPUT after a file import. If a value was read in as text by PROC IMPORT, INPUT is the usual way to turn it into a number. If a string holds several pieces of information, extract the part you need first, for example with SUBSTR, and then convert it.
How to install or access SAS
INPUT is a SAS function, not an add-on package, so there is nothing separate to install: you run it in a DATA step in whichever SAS session you already use. The SAS Support site (support.sas.com) hosts the Knowledge Base sample used in this guide, and the SAS 9.4 Formats and Informats reference is where the informats are listed by type. Your institution’s SAS licence, or whichever SAS environment your research group uses, determines how you start a session; use the access route your site provides.
How to convert character to numeric in SAS step by step
- Confirm the variable is character. Run
proc contents data=your_data; run;and look at the Type column. A variable that displays like a date or a number may already be numeric with a format applied, in which case it does not need INPUT. Details of reading the listing are in the PROC CONTENTS guide. - Look at how the text is written. Plain digits, digits with commas, a dollar sign, or a date such as 12JUL2016 each need a different informat. The table below pairs each pattern with the informat shown in the SAS sources.
- Choose the informat. The informat tells SAS how to interpret the character data. The SAS Formats and Informats reference lists the choices by type, so you can see which one matches your data.
- Write the INPUT call into a new variable. The general form from SAS Knowledge Base sample 24590 is
new_variable = input(original_variable, informat.);. For a simple string of digits:data new; char_var = '12345678'; numeric_var = input(char_var, 8.); run; - Keep the original variable name if you need to. A variable can be defined as only one type, so the same name cannot be reused in place. Drop the original and rename the new variable:
data new; orig = '12345678'; new = input(orig, 8.); drop orig; rename new=orig; run; - Apply a format only for display. The stored value is a number; a format changes how it prints. To show leading zeros that were in the text, convert with a numeric informat and then apply the Z format, for example
format numeric_var z4.;for a four-digit value such as 0012. - Re-run PROC CONTENTS. The new variable should now be listed as Num.
Which informat for which text pattern
The data table below lists each text pattern, the informat the SAS sources use for it, and what the stored value is. It is compiled from the SAS Knowledge Base sample and the SAS 9.4 reference, and the example in each row is the one printed there.
Converting a character date to a SAS date
A SAS date is a numeric value that can be used with date functions and calculations, even when it displays with letters. If a data set holds the string 12JUL2016 in a character variable, look at the date informats to find one that matches. DATE9. reads values in the form ddmmmyy that matches 12JUL2016, so:
startdate = "12JUL2016";
date_var = input(startdate, date9.);
format date_var date9.;
The result looks like the original text but can be analysed with date functions. The same method applies to time and datetime values. For choosing the display format afterwards, see date formats in SAS.
Numbers with commas or dollar signs
When a string contains non-digits such as commas or dollar signs, a plain 8. informat is not the right choice. The Knowledge Base shows input(char_var, comma10.) for the text 12,000,000 and input(char_var, dollar10.) for $6,000,000. In both cases the number is saved with only digits, so printing the variable shows 6000000 unless you apply a format such as format numeric_var dollar10.;.
How to read the output
Three things tell you whether the conversion worked: the variable type, the values, and the log.
The variable type in PROC CONTENTS
In the PROC CONTENTS listing, a character variable has type Char and a numeric one has type Num. The SSCC example shows a character variable strzip listed as Char with length 8 before conversion. After INPUT, the new variable should be listed as Num. If it is still Char, you most likely applied a character informat (one starting with $) or reused a variable name.
The values
Print a few rows and compare them with the source text. A value that is missing where the text was not blank means the informat did not match that value. Do not treat a clean-looking print as proof for all rows: count missing values in the new variable and compare with the count in the old one, so conversion failures are not hidden among legitimate blanks.
The two different log notes
Two log notes are easy to confuse. The first appears when SAS converts types for you. The SSCC page shows that using character data with an arithmetic operator, as in intzip1 = strzip+0;, produces a note of the form “Character values have been converted to numeric values at the places given by: (Line):(Column).” SSCC states that these notes can largely be ignored but that the clutter can make it hard to see more consequential notes, and recommends explicit INPUT or PUT conversion to avoid them.
The second appears when INPUT meets a value that does not fit the informat: “NOTE: Invalid argument to function INPUT at line 725 column 8.” (the line and column are from the Knowledge Base example). This note means that at least one value became missing, so it deserves attention rather than suppression.
Troubleshooting character-to-numeric conversion in SAS
All or some values come out missing
According to the Knowledge Base, if you choose an informat that does not match your data, or some values do not match, INPUT returns a missing value and writes the Invalid argument note to the log. Compare a failing value with the table above: a comma, a currency sign or a date written as text each needs its own informat.
You want fewer notes for known bad data
If you know the original variable contains some invalid data and you do not want every note in the log, the Knowledge Base says you can suppress the printing of these notes with the ? or ?? modifier in front of the informat: numeric_var = input(char_var, ? 8.);. This hides the notes, not the missing values, so check the result for missing values yourself afterwards.
The variable is still character after the step
A variable has one type only. Writing x = input(x, 8.); does not change x to numeric; create a new variable and use DROP and RENAME as shown above.
Leading zeros are lost
A numeric value does not hold leading zeros. If the zeros are an identifier rather than a quantity, as in a ZIP code, SSCC suggests keeping it as a character variable. In the reverse direction, SSCC shows put(intzip, z5.0) with length strzip1 $5; to turn the numeric value 1 into 00001.
A literal string causes an error
If the first argument to INPUT is a character expression rather than a variable name, it must be enclosed in quotation marks, as the SAS Formats and Informats reference notes.
Free alternatives to SAS for this conversion
The same conversion exists in other statistical tools. The rows below come from the tools’ own reference pages.
| Tool | Function | What the reference page says |
|---|---|---|
| SAS | input(x, informat.) |
Converts a character expression using an informat; mismatched values become missing. |
| R | as.numeric(x) |
Coerces an object to numeric; as.numeric is identical to as.double. |
| Python (pandas) | pandas.to_numeric(arg, errors='raise') |
Converts an argument to a numeric type; with errors='coerce', invalid parsing is set to NaN, and with the default 'raise' it raises an exception. |
One difference worth knowing when moving a SAS script: SAS makes you name the informat for each text pattern, so a comma-formatted string needs comma10., whereas the R and pandas pages describe a single function with no informat argument.
Sources
- SAS Support, Sample 24590: Convert variable values from character to numeric or from numeric to character
- SAS 9.4 Formats and Informats: Reference, Using Informats
- Social Science Computing Cooperative, University of Wisconsin-Madison: Numeric and Character Type Conversions in SAS
- pandas documentation: pandas.to_numeric
- R documentation: Numeric Vectors (numeric, as.numeric)
Data: SAS informats for converting character text to numeric values, by text pattern
Maps each character text pattern (plain digits, decimal, comma-separated, dollar, date) to the informat used with the SAS INPUT function and the stored numeric result, compiled from SAS Support Knowledge Base sample 24590 and the SAS 9.4 Formats and Informats reference.
| Text pattern | Example text | INPUT call | Stored result |
|---|---|---|---|
| Plain digits | 12345678 | input(char_var, 8.) | 12345678 |
| Decimal number | 98.6 | input(TempCharacter, 4.) | 98.6 |
| Digits with commas | 12,000,000 | input(char_var, comma10.) | 12000000 |
| Dollar sign and commas | $6,000,000 | input(char_var, dollar10.) | 6000000 |
| Date as text | 12JUL2016 | input(startdate, date9.) | SAS date (numeric); display with format date9. |
Sources
- SAS Sample 24590: Convert variable values from character to numeric (accessed 2026-11-17)
- SAS 9.4 Formats and Informats: Reference, Using Informats (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







