In an existing dataset, is there a way to change a column of dates in the format of "mm/dd/yyyy" to character format of "mmddyyyy"? thanks.
data have;
informat date mmddyy10.;
format date mmddyy10.;
input date;
cards;
01/31/2012
;
data want;
set have;
ndate=put(date,mmddyyn.);
proc print;run;
or
data want;
set have(rename=date=ndate) ;
date=put(ndate,mmddyyn.);
drop ndate;
proc print;run;
Obs date
1 01312012
data have;
informat date mmddyy10.;
format date mmddyy10.;
input date;
cards;
01/31/2012
;
data want;
set have;
ndate=put(date,mmddyyn.);
proc print;run;
or
data want;
set have(rename=date=ndate) ;
date=put(ndate,mmddyyn.);
drop ndate;
proc print;run;
Obs date
1 01312012
Linlin, thanks! it's perfect.
the date converted mmddyyyyy seems to be still numeric. is there a way to change it to Character mmddyyyy ?
i tried
ndob=put(dob, $8.);
the ndob became negative number that does not look like a date any more.
Please post the complete code that you have run. LinLin' code meets your requirement.
Haikuo
data xyz2;
set xyz1;
format olddate mmddyyn.;
run;
/*Now the olddate changed from mm/dd/yyyy to mmddyyyy in new dataset xyz2
but the variable type is still numeric.
the question is how to change the mmddyyyy to character type? */
/* i tried the following to change oldate to a new column named newdate and change it to character type, but newdate became negative value that does not look like a date but a number only. how to make mmddyyyy from numeric to characters mmddyyyy? */
newdate=put(olddate, $8.);
Try this, btw, LinLin already has this in her code:
newdate=put(olddate, mmddyyn.);
The first format you used is just a format change, not a type change, so olddate will still be the way it is, numeric. You need to involve a new variable ( you have tried) to fix this.
Haikuo
the following works:
data xyz2;
set xyz1;
newdate=put(olddate, mmddyyn.);
run;
thanks.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.