Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

Accepted Solution Solved
Reply
Contributor
Posts: 37
Accepted Solution

changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

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.


Accepted Solutions
Solution
‎04-18-2012 11:56 AM
Super Contributor
Posts: 1,636

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

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

View solution in original post


All Replies
Solution
‎04-18-2012 11:56 AM
Super Contributor
Posts: 1,636

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

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

Contributor
Posts: 37

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

Linlin, thanks!   it's perfect.

Contributor
Posts: 37

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

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.

Respected Advisor
Posts: 3,124

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

Please post the complete code that you have run. LinLin' code meets your requirement.

Haikuo

Contributor
Posts: 37

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

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.);

Respected Advisor
Posts: 3,124

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

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

Contributor
Posts: 37

Re: changing from a date format "mm/dd/yyyy" to character format "mmddyyyy"

the following works:

data xyz2;

set xyz1;

newdate=put(olddate, mmddyyn.);

run;

thanks.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 10823 views
  • 4 likes
  • 3 in conversation