BookmarkSubscribeRSS Feed
Dg112
Calcite | Level 5
My Data has the following date values in character variable type
Date
2013/12
2014/01
2014/02
2014/02
2012/06

How should I convert the variable type to numeric?
1 REPLY 1
Kurt_Bremser
Super User

Since you cannot change the type of a variable, you have to create a new one, which is done by doing the "rename-convert-drop" dance:

data have;
input date $7.;
datalines;
2013/12
2014/01
2014/02
2014/02
2012/06
;

data want;
set have (rename=(date=_date));
format date yymms7.;
date = input(compress(_date,"/"),yymmn6.);
drop _date;
run;

(SAS will automatically assume the first day of the month for the raw date values)

Alternatively, you can add the day yourself and use a complete date informat:

data want;
set have (rename=(date=_date));
format date yymms7.;
date = input(_date!!"/01",yymmdd10.);
drop _date;
run;

Or you can use the same method when initially bringing the data into SAS, which depends on the type of your data source.