DATA Step, Macro, Functions and more

how to convert character date (displaying as mmddyy10 format) into numeric date (format datetime20. )

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

how to convert character date (displaying as mmddyy10 format) into numeric date (format datetime20. )

I have data set dtset1.

sn.  stdt;

1  10/10/2010

2  10/19/2011

stdt is a character variable with length 10.

I need to convert this into numeric date like datetime20.

I tried this;

data new-dtset;

set dtset1 (rename=(stdt=stdt_n));

stdt=input(stdt_n, date9.);

format stdt datetime20.;

drop stdt_n;

run;

but It didn't work, It's converting to numberic but showing as missing value.

I need help to solve this problem.

Thank you


Accepted Solutions
Solution
‎05-11-2015 08:08 AM
Super User
Posts: 6,939

Re: how to convert character date (displaying as mmddyy10 format) into numeric date (format datetime20. )

Replace

stdt=input(stdt_n, date9.);

with

stdt = input(stdt_n,mmddyy10.) * 86400;

Since you want it to display as a datetime value, it needs to count the seconds from 01jan1960 instead of just the days.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
SAS Employee
Posts: 20

Re: how to convert character date (displaying as mmddyy10 format) into numeric date (format datetime20. )

First you need to change the informat reading the date and secondly you must convert that date value into a datetime value using the dhms function.

data newdtset;

set dtset1 (rename=(stdt=stdt_n));

stdt=dhms(input(stdt_n, mmddyy10.),0,0,0);

format stdt datetime20.;

drop stdt_n;

run;

Regards,

Michael

Contributor
Posts: 40

Re: how to convert character date (displaying as mmddyy10 format) into numeric date (format datetime20. )

Thank you Michael

Solution
‎05-11-2015 08:08 AM
Super User
Posts: 6,939

Re: how to convert character date (displaying as mmddyy10 format) into numeric date (format datetime20. )

Replace

stdt=input(stdt_n, date9.);

with

stdt = input(stdt_n,mmddyy10.) * 86400;

Since you want it to display as a datetime value, it needs to count the seconds from 01jan1960 instead of just the days.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 9,681

Re: how to convert character date (displaying as mmddyy10 format) into numeric date (format datetime20. )

Directly use informat .

option datestyle=mdy;
data have;
input sn  stdt : $20.;
d=input(stdt,anydtdtm20.);
format d datetime.;
cards;
1  10/10/2010
2  10/19/2011
;
run;

Xia Keshan

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 635 views
  • 3 likes
  • 4 in conversation