Hello,
I have seen some posts about splitting text strings into multiple variables, including some date/time strings, but I have not found a solution that would work for my problem. I am importing device data that has multiple variables and one variable for date and time (called DATE_TIME). I need the string to be parsed out into date (formatted like 02/21/2019, so mmddyy10.) and time.
An example of the string I am working with:
20181002105310
This would be 10/02/2018 at 10:53:10.
If it is helpful - there are about 728 observations in this dataset, and I need to do this for two variables (DATE_TIME and TRANSMISSION_DATE_TIME) so it would be nice to be able to do this efficiently if possible 🙂
You will probably need to convert the data and time parts separately.
data test;
str='20181002105310';
date = input(str,yymmdd8.);
time = input(substr(str,9),hhmmss6.);
format date yymmdd10. time time8.;
put (_all_) (=);
run;
str=20181002105310 date=2018-10-02 time=10:53:10
Input the string as a numeric SAS date/time value using the ANYDTDTM informat.
An example is shown at that link.
From there, parsing out the date is done easily with the DATEPART function, parsing out the time is done easily with the TIMEPART function.
Try
date = input(substr(string,1,8),yymmdd8.);
time = input(substr(string,9,6),time6.);
format
date yymmddd10.
time time8.
;
You will probably need to convert the data and time parts separately.
data test;
str='20181002105310';
date = input(str,yymmdd8.);
time = input(substr(str,9),hhmmss6.);
format date yymmdd10. time time8.;
put (_all_) (=);
run;
str=20181002105310 date=2018-10-02 time=10:53:10
This works great! Do I need to make a macro so that the str variable will update?
Ex) the first DATE_TIME is 20181002105310 which comes out to date: 2018-10-02 time: 10:53:10
the next DATE_TIME is 20170509073215 and I would need the same type of output for date and time, is there a way to make a loop where SAS will just read all of these DATE_TIME variables and convert them accordingly?
A data step is a loop fed by input data (either from a dataset or from an external file).
You're misunderstanding what I said. I was regarding the "str" variable for the string. Right now what that would do is autofill the same str variable for each DATE_TIME variable and subsequently each new output for date and time. I am asking how to make it so that a new str value will be produced for each DATE_TIME variable input.
I figured it out using this code - thank you!
Changed it to:
Data PARI; set xml_files;
date = input(DATE_TIME,yymmdd8.);
time = input(substr(DATE_TIME,9),hhmmss6.);
format date mmddyy10. time time8.;
put (_all_) (=);
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.