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.
Try this:
data _null_;
str = "2008-01-08 07:33:00";
dt = input(str, anydtdtm.);
put dt :datetime.;
run;
PG
Try this:
data _null_;
str = "2008-01-08 07:33:00";
dt = input(str, anydtdtm.);
put dt :datetime.;
run;
PG
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;
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
Not sure why I thought this wouldn't work with my data, but turns out it does. Thanks.
Thanks Everyone.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.