DATA Step, Macro, Functions and more

How to read this date format?

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 86
Accepted Solution

How to read this date format?


Hi

Can you please help me with this query

How can I read this in SAS from a text file. This is in YYYY-MM_DD HH:MMSmiley FrustratedS

2008-01-08 07:33:00

Or there is no informat to read this.


Accepted Solutions
Solution
‎02-20-2014 09:53 AM
Respected Advisor
Posts: 4,663

Re: How to read this date format?

Try this:

data _null_;

str = "2008-01-08 07:33:00";

dt = input(str, anydtdtm.);

put dt :datetime.;

run;

PG

PG

View solution in original post


All Replies
Solution
‎02-20-2014 09:53 AM
Respected Advisor
Posts: 4,663

Re: How to read this date format?

Try this:

data _null_;

str = "2008-01-08 07:33:00";

dt = input(str, anydtdtm.);

put dt :datetime.;

run;

PG

PG
Occasional Contributor
Posts: 10

Re: How to read this date format?

You would have to modify this somewhat, but here is a macro I wrote for a similar problem.  In my case, the input dates were character vars and all

"DD/MM/YYYY hh:mm" For me, it was important to be able to set impossible dates to missing.  I also use the year variable to create a calendar year class var, which can be helpful.

%MACRO MKSASDT(DATEVAR,  NEWDTVAR, FIRSTYR, LASTYR, I);

_&I._TEMPDT = COMPRESS(&DATEVAR);

_&I._TEMPDT = STRIP(_&I._TEMPDT);

_&I._MM = SUBSTR(_&I._TEMPDT, 1,2);

_&I._DD = SUBSTR(_&I._TEMPDT, 4,2);

_&I._YY = SUBSTR(_&I._TEMPDT, 7,4);

_&I._HH = SUBSTR(_&I._TEMPDT, 11,2);

_&I._TT = SUBSTR(_&I._TEMPDT, 14,2);

IF _&I._YY<&FIRSTYR OR _&I._YY>&LASTYR THEN _&I._YY=.;*GET RID OF WAY PAST AND FUTURE DATES;

&NEWDTVAR._DT = MDY(_&I._MM, _&I._DD, _&I._YY);

&NEWDTVAR._DTM = DHMS(&NEWDTVAR._DT, _&I._HH, _&I._TT, 0);

FORMAT &NEWDTVAR._DT DATE9. &NEWDTVAR._DTM DATETIME20.;

%MEND;

Occasional Contributor
Posts: 7

Re: How to read this date format?

the informat anytdtm will read you data.

Try something like:

Data _null_;

    infile cards;

    input datetme anydtdtm20.;

    put datetme datetime20.;

cards;

2008-01-08 07:33:00

;;;;

run;

The log shows the new formatted value:  

  08JAN2008:07:33:00

Occasional Contributor
Posts: 10

Re: How to read this date format?

Not sure why I thought this wouldn't work with my data, but turns out it does.  Thanks.

Frequent Contributor
Posts: 86

Re: How to read this date format?

Thanks Everyone.

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 401 views
  • 8 likes
  • 4 in conversation