BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
bnarang
Calcite | Level 5


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:MM:SS

2008-01-08 07:33:00

Or there is no informat to read this.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

5 REPLIES 5
PGStats
Opal | Level 21

Try this:

data _null_;

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

dt = input(str, anydtdtm.);

put dt :datetime.;

run;

PG

PG
sullivan0822
Calcite | Level 5

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;

Wizard
Calcite | Level 5

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

sullivan0822
Calcite | Level 5

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

bnarang
Calcite | Level 5

Thanks Everyone.

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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