In this article we have already seen how you can convert char variable to numeric and numeric variable to character variable.
When it comes to DATE in SAS, it’s a bit different because SAS has its own date format and informats.
You can use the INPUT() function to convert a character date to a numeric date variable in SAS. Please note that DATE is getting stored in SAS as a numeric value. You can format that date in multiple ways to make it a common readable format.
This function uses the following simple syntax:
date_var = input(character_var, informat.);
format date_var format.;
The following examples show how you can use this function in the SAS code.
Convert character Date variable to SAS numeric Date
Assume that you can character value “11052020” on char variable “character_var“. Let’s convert it into SAS DATE date9. format
data new;
character_var="11052020";
date_var=input(character_var, DDMMYY10.);
format date_var date9.;
proc print;
run;
Example 2:
Suppose we have the following employee dataset in SAS where DOB is a character variable which has date values but in char string.
Let’s create a new dataset with a new numeric variable numDate_DOB for DOB which will be a numeric variable but holds SAS DATE values in date9. format.
/* create a SAS data set*/
data employee;
input employeeID $ 1-4 DOB $ 5-14 Name $ 15-29;
datalines;
0001 01APR1990 John Doe
0002 12OCT1980 Jane Smith
0003 09FEB1997 Bob Johnson
0004 10SEP1999 Emily Davis
0005 26JAN1981 Michael Brown
0006 01NOV1897 Sarah Miller
0007 12MAY1995 David Garcia
0008 18JUL1993 Jessica Wilson
0009 18AUG1996 James Moore
0010 10DEC1998 Melissa Taylor
;
proc print;
run;
Check the data type of DOB variable:
/* check the data types of variables */
proc contents data=employee;
run;
Convert character Date into numeric Date in SAS using INPUT() function.
data new;
set employee;
numDate_DOB=input(DOB, date9.);
format numDate_DOB date9.;
proc print;
run;
/* check the data type of DOB & numDate_DOB variables */
proc contents data=new;