SAS: Convert Character to Number (and back)
A common task is to convert characters variables to numeric variables. Sometimes its more convenient to have a number appear as a character.
Change a Numeric Variable into Character Variable
You can convert a numeric variable to a character variable using the PUT function. The PUT function converts numeric values to character values based on a specified format.
/* create a numeric variable */
data work.mydata;
input numvar;
datalines;
123
456
789
;
run;
/* convert numeric variable to character variable */
data work.mydata_char;
set work.mydata;
charvar = put(numvar, 5.);
run;
/* view the results */
proc print data=work.mydata_char;
run;
In this example, the PUT function is used to convert the numeric variable “numvar” to a character variable called “charvar”. The format “5.” is specified, which means that the resulting character variable should be 5 characters in length, padded with leading blanks if necessary.
Change a Character Variable into Numeric Variable
When you read data from a flat file (such as CSV or TXT), it may come into the dataset as character. To convert it to numeric, use the INPUT function.
With the INPUT function you provide the variable name and then the numeric format.
/* create a character variable */
data work.mydata;
input charvar $;
datalines;
123
456
789
;
run;
/* convert character variable to numeric variable */
data work.mydata_num;
set work.mydata;
numvar = input(charvar, 5.);
run;
/* view the results */
proc print data=work.mydata_num;
run;
The numeric_format argument can be any of the numeric formats that are available in SAS, such as “8.” for integers or “10.2” for numbers with two decimal places. The format specifies the width and precision of the resulting numeric value, and it must match the format of the character value being converted.
If the character value being converted does not match the specified format, the INPUT function may return a missing value or an error message. You gotta get it right!
What About Dates?
If your date variable arrives as text, use the INPUT function to convert it. Again the tricky part is understanding which format to use. The date format tells SAS how to interpret the text string as a date, and the INPUT function converts the text string to a numeric date value.
Here’s an example of how to use the INPUT function to convert a date stored as text to a numeric date value using the DATE9 format.
/* create a character variable with dates stored as text */
data work.mydata;
input datechar $;
datalines;
01-JAN-2000
15-FEB-1960
28-MAR-1983
;
run;
/* convert character variable to numeric date variable */
data work.mydata_dates;
set work.mydata;
numdate = input(datechar, date9.);
run;
/* view the results */
proc print data=work.mydata_dates;
run;
Working with More Complicated Dates
When inputting date variables, remember it goes from easy to complicated faster than you can say “I Love Statistics Homework”. There are two format that can handle some of the heavy lifting: ANYDTDTE. and ANYDTDT?.
The ANYDTDTE informat is a special type of SAS informat that can be used to read dates in a wide range of formats. It automatically detects the format of the input date and converts it to a SAS date value. Here’s an example using the format. Notice the different date values we are using for input.
/* create a character variable with dates stored as text */
data work.mydata;
input datechar $;
datalines;
01/01/2022
02-15-2022
2022-03-28
;
run;
/* convert character variable to SAS date variable */
data work.mydata_date;
set work.mydata;
datevar = input(datechar, anydtdte.);
run;
/* view the results */
proc print data=work.mydata_date;
run;
But look how easy it made the work, here’s the example output. It didn’t pay any attention to the dividing marks or where the year appeared.
datechar datevar
01/01/2022 01JAN2022
02-15-2022 15FEB2022
2022-03-28 28MAR2022
This format also works with date time or timestamp values. In this case, add the character length to get a better result.
/* create a character variable with datetime values stored as text */
data work.mydata;
input datetimechar $;
datalines;
01/01/2022 10:30:45
02-15-2022 12:45:15
2022-03-28 09:20:30
;
run;
/* convert character variable to SAS datetime variable using the ANYDTDTE informat */
data work.mydata_datetime;
set work.mydata;
datetimevar = input(datetimechar, anydtdte20.);
run;
/* view the results */
proc print data=work.mydata_datetime;
run;
In this example, the INPUT function is used with the ANYDTDTE20. informat to convert the character variable “datetimechar” to a SAS datetime variable called “datetimevar”. The ANYDTDTE20. informat indicates that the datetime values can be in various formats, including “mm/dd/yyyy hh:mm:ss”, “dd-mm-yyyy hh:mm:ss”, or “yyyy-mm-dd hh:mm:ss”. The resulting output would look like this:
datetimechar datetimevar
01/01/2022 10:30:45 01JAN2022:10:30:45
02-15-2022 12:45:15 15FEB2022:12:45:15
2022-03-28 09:20:30 28MAR2022:09:20:30
Keep This in Mind
Note that the resulting SAS date value is stored as the number of days since January 1, 1960, and can be used in various SAS date functions and formats.
Americans prefer dates with the month first, while other nations use the month in the middle. A date such as 12/1/2022 may be Jan 1 or Dec 12 depending on where the data was created.
Another smart feature of SAS is that it reads the date values based on your DATESTYLE option, which looks at the LOCALE of your system.
Summary
Make sure you have a firm grasp on how these functions work. If you are reading in raw data, your fingers will be typing them often.