How to Convert Character Variables to Numeric in SAS
Use the INPUT function with an informat that matches the text you have to convert a character variable to numeric in SAS. The key is to choose the informat from the characters actually present, plain digits, commas, dollar signs, or a date representation, not merely from how the value looks when printed. This guide shows the safe DATA step method, how to retain a familiar variable name, and how to verify that SAS created a usable numeric value.
Character and Numeric Values in SAS
SAS has two fundamental variable types: character and numeric. A character value such as "1250" looks like a number but is still text, so you cannot reliably use it in arithmetic, date calculations, or numeric sorting until you convert it.
The INPUT function reads character text and returns a numeric value. An informat tells SAS how to interpret that text. SAS’s official conversion guidance uses this general pattern:
new_variable = input(original_variable, informat.);
Do not force an automatic conversion with an expression such as char_var + 0. SAS may perform an implicit conversion, but it writes conversion notes to the log and can make data-quality problems easier to miss.
Convert a value to numeric only when it represents a quantity, calculation input, or date. Identifiers such as ZIP codes, account numbers, and product codes often need to remain character because their leading zeros and exact text form can matter.
Convert Character Text to Numeric in a DATA Step
A DATA step is the clearest and most dependable way to create a numeric version of a character variable.
Step 1: Inspect the Source Variable
- Confirm that the source column is character.
- Check its displayed values for commas, currency symbols, decimal points, blanks, or placeholder values.
- Look at the variable’s current format before assuming a date-looking value is already a SAS date.
Run PROC CONTENTS to see the variable type, length, and assigned formats. Print representative values if needed.
proc contents data=work.have;
run;
proc print data=work.have;
var char_var;
run;
If PROC CONTENTS lists the variable as Char, use INPUT to create a numeric variable. If it lists the variable as Num, it is already numeric, though it may still need a different display format.
Step 2: Choose the Informat That Matches the Text
Use this table to match the source text to the informat SAS should use.
| Character value example | Informat to use | What SAS reads |
|---|---|---|
12345678 |
8. |
A simple digit string up to 8 characters wide |
1,234.50 |
comma10. |
A number containing commas |
$6,000,000 |
dollar10. |
A dollar-formatted value |
12JUL2016 |
DATE9. |
A character date in ddMONyyyy form |
The width in an informat is the number of characters SAS can read. Make it wide enough for the longest valid value in the source field, including commas, decimal points, signs, and currency symbols.
For a simple digit string such as "12345678", use the 8. informat:
data work.want;
set work.have;
count_n = input(count_c, 8.);
run;
For an amount stored as character text such as "1,234.50", use comma10.:
data work.want;
set work.have;
amount_n = input(amount_c, comma10.);
run;
For values containing a dollar sign, use dollar10.:
data work.want;
set work.have;
sales_n = input(sales_c, dollar10.);
run;
Choose a report format after conversion when a particular display style is needed. Display formatting and stored numeric values are separate things in SAS.
Step 3: Flag Bad Values Instead of Quietly Losing Them
A blank character value normally becomes a numeric missing value. A nonblank value that does not match the informat can also become missing, so keep the original character variable until you review the result.
During initial cleanup, run the conversion without modifiers and inspect the SAS log for invalid values. Once you have a process for identifying bad rows, the ?? modifier can suppress repeated invalid-data notes while you create a clear flag.
data work.want;
set work.have;
amount_n = input(amount_c, ?? comma10.);
conversion_problem =
not missing(strip(amount_c)) and missing(amount_n);
run;
This example flags rows where amount_c contains nonblank text but SAS could not produce a numeric amount_n. Review these rows before using the numeric column in reports or calculations.
Step 4: Verify the Converted Variable
Finish by checking that SAS created a numeric column and that unexpected conversion failures are accounted for.
proc contents data=work.want;
run;
proc freq data=work.want;
tables conversion_problem / missing;
run;
In PROC CONTENTS, the new variable should show Num as its type. If it is still character, check the assignment: the INPUT function must be on the right side of the expression that creates the new variable.
Keep the Original Variable Name
SAS cannot change a variable’s type in place within an existing data set. To keep the original column name, rename the incoming character variable temporarily, create a new numeric variable under the original name, then drop the temporary source.
This example converts a character column named sales that contains dollar-formatted values:
data work.want;
set work.have(rename=(sales=sales_c));
sales = input(sales_c, dollar10.);
drop sales_c;
run;
The output data set contains a numeric variable named sales. The original character sales variable is not retained.
For a report that needs a four-character display, apply z4. after conversion: For example, a character value of "0012" becomes numeric 12, while z4. displays it as 0012:
data work.want;
set work.have;
code_n = input(code_c, 4.);
format code_n z4.;
run;
Convert Character Dates to SAS Dates
A SAS date is numeric internally, even though it is usually displayed with letters, slashes, or hyphens. Converting a character date to a SAS date lets you sort dates correctly, calculate durations, and use functions such as YEAR, MONTH, and INTCK.
For a character date stored as 12JUL2016, use the DATE9. informat and then apply a date format for display:
data work.want;
set work.have;
start_dt = input(startdate_c, DATE9.);
format start_dt DATE9.;
run;
For character dates in YYYY-MM-DD form, use the YYMMDD10. informat:
data work.want;
set work.have;
if strip(date_c) in ('', '.') then date_n = .;
else date_n = input(date_c, yymmdd10.);
format date_n yymmdd10.;
run;
Character date fields with YYYY-MM-DD entries may use a period as a placeholder for a missing date. Treat those placeholder values as missing before applying the date conversion, rather than sending them through the date informat.
Do not assume that a numeric value that looks like 10122012 is a SAS date. If PROC CONTENTS shows it is numeric but has no date format, it is simply the integer 10122012. If the value is intended to mean an MMDDYYYY date, convert its digits to character text first and then read it with the appropriate date informat.
data work.want;
set work.have;
sas_date = input(put(raw_date, z8.), mmddyy8.);
format sas_date mmddyy10.;
run;
This is appropriate only when the business meaning of the eight digits is known to be an MMDDYYYY date.
Convert Character to Numeric in PROC SQL
You can use the same INPUT function in SAS PROC SQL. This is useful when creating a new table as part of a query.
proc sql;
create table work.want as
select
h.*,
input(h.amount_c, comma10.) as amount_n
from work.have as h;
quit;
This keeps the original amount_c column and adds the numeric amount_n column. As with a DATA step, choose the informat from the actual source text and validate missing or invalid results before relying on the new variable.
Verify and Prevent Conversion Problems
Most character-to-numeric issues come from choosing an informat that does not match the source text.
Use this checklist after each conversion:
- Confirm the source variable is character and the new variable is numeric with
PROC CONTENTS. - Compare original and converted values for a sample of rows.
- Check the SAS log for invalid-data messages during cleanup.
- Flag nonblank character values that became numeric missing values.
A numeric value can display with commas, currency symbols, dates, or leading zeros, but its storage type remains numeric. An informat reads text into a numeric value; a format controls how that numeric value is shown.
For your next conversion, start with PROC CONTENTS, inspect the literal characters in the source column, then use INPUT with the matching informat and validate the resulting numeric variable.
Frequently Asked Questions
How do I convert a character variable to an integer in SAS?
Use INPUT to create a numeric value; SAS numeric variables can represent whole numbers and decimals rather than using a separate integer variable type. For a digit string such as "12345678", use input(char_var, 8.) when the source fits an 8-character field.
integer_value = input(char_var, 8.);
If the text includes commas, dollar signs, or other symbols, use an informat that recognizes those characters instead.
Why does SAS show “Invalid argument to function INPUT” in the log?
The character value does not match the informat you selected. For example, 8. is not appropriate for text such as "1,250" or "$1,250"; use comma10. or dollar10. based on the characters in the source value.
Inspect the original text, correct or flag invalid rows, and preserve the character source field until the conversion is complete.
Can I overwrite a character variable with a numeric variable in SAS?
Not directly in the same data set, because SAS assigns a variable’s type when the data set is created. Rename the incoming character variable, create a new numeric variable with the original name, and drop the temporary character variable only after validation.
Why did leading zeros disappear after conversion?
Numeric values do not store leading zeros. If the value is numeric and needs a four-character display, apply format numeric_var z4.; if the zeros are part of an identifier, leave the variable as character.
How do I know whether a date-looking SAS variable is already a SAS date?
Run PROC CONTENTS and inspect both its type and format. A real SAS date is numeric and commonly has a date format assigned; a bare numeric value such as 10122012 with no date format is an integer, not automatically a usable SAS date.
How can you check whether a character variable will convert to a number in SAS?
Create a numeric test variable with INPUT and an informat suited to the characters in the source value. If an entry cannot be read by that informat, the test result is missing and SAS writes an invalid-argument note to the log; review those source entries before using the converted field.
