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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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