I have a data where it has individual column for:
Day, Month, Year, and time.
For time, the source data is 0900 for 9 o clock and weirdly, they have 10000 value.
When formatting that, it can be converted into 10:00. But when i convert it into datetime, it gave warning:
NOTE: Invalid argument to function INPUT at line 45 column 16.
year=2014 month=January transformer=T1 Day=Wednesday Date=1 Time=10:00:00 numMonth=1 sasdate=01JAN2014 newdatetime=. _ERROR_=1 _N_=39
Here are my codes:
data converttodate; set finaltestone; if month = 'January' then numMonth = 1; if month = 'February' then numMonth = 2; if month = 'March' then numMonth = 3; if month = 'April' then numMonth = 4; if month = 'May' then numMonth = 5; if month = 'June' then numMonth = 6; if month = 'July' then numMonth = 7; if month = 'August' then numMonth = 8; if month = 'September' then numMonth = 9; if month = 'October' then numMonth = 10; if month = 'November' then numMonth = 11; if month = 'December' then numMonth = 12; sasdate=mdy(numMonth,date,year); format sasdate date9.; /* preptime = substr(time,1,4);*/ format time time.; attrib newdatetime format=datetime19.; newdatetime=input(put(sasdate,date9.)||put(time,time.),datetime.); run;
Weirdly, the when the time is 10:00, it will never be able to produce datetime and hence prompting warning in the log for newdatetime.
Does anyone know what could be the cause of it?
Use an informat for the month conversion, and your code will become much cleaner:
data have;
input year month :$10. date time :time8.;
format time time8.;
cards;
2014 January 1 10:00:00
;
run;
proc format;
invalue inmonth
'January' = 1
'February' = 2
'March' = 3
'April' = 4
'May' = 5
'June' = 6
'July' = 7
'August' = 8
'September' = 9
'October' = 10
'November' = 11
'December' = 12
;
run;
data want;
set have;
format newdatetime datetime19.;
newdate = mdy(input(month,inmonth.),date,year);
newdatetime = dhms(newdate,0,0,time);
drop newdate;
run;
To do the conversion with the datetime informat, you need a colon between the date and the time, e.g.:
newdatetime=input(put(sasdate,date9.)||':'||put(time,time.),datetime.);
or you can use the dhms function:
newdatetime=dhms(sasdate,hour(time),minute(time),second(time));
Date values are the number of days since 01JAN1960. Datetimes are the number of seconds since 01JAN1960:00:00:00. So another possibility is to turn the number of days into seconds and add it to the time:
newdatetime=sasdate*24*60*60+time;
Use an informat for the month conversion, and your code will become much cleaner:
data have;
input year month :$10. date time :time8.;
format time time8.;
cards;
2014 January 1 10:00:00
;
run;
proc format;
invalue inmonth
'January' = 1
'February' = 2
'March' = 3
'April' = 4
'May' = 5
'June' = 6
'July' = 7
'August' = 8
'September' = 9
'October' = 10
'November' = 11
'December' = 12
;
run;
data want;
set have;
format newdatetime datetime19.;
newdate = mdy(input(month,inmonth.),date,year);
newdatetime = dhms(newdate,0,0,time);
drop newdate;
run;
After converting to newdatetime, i then tried to use it in a new data step but the datetime will never be displayed properly again.
Here is my code :
proc format;
invalue inmonth
'January' = 1
'February' = 2
'March' = 3
'April' = 4
'May' = 5
'June' = 6
'July' = 7
'August' = 8
'September' = 9
'October' = 10
'November' = 11
'December' = 12
;
run;
data converttodate;
set finaltestone;
format newdatetime datetime19.;
newdate = mdy(input(month,inmonth.),date,year);
newdatetime = dhms(newdate,0,0,time);
drop newdate;
run;
data work.finaltesthash;
set orifile;
length excp_code $50;
attrib station length=$10;
attrib voltage length=$10;
attrib year length=8;
attrib month length=$20;
attrib transformer length=$10;
attrib newdatetime length=8 format=datetime19.;
run;
all data under newdatetime became . in work.finaltesthash. It was displaying properly in work.converttodate.
Anyone can help me with this?
Maxim 2: read the log.
newdatetime is unititialized because you used dataset orifile, and not dataset converttodate.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.