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

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 🙂
 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

Input the string as a numeric SAS date/time value using the ANYDTDTM informat.

https://documentation.sas.com/?docsetId=leforinforref&docsetTarget=p1hsn1ji141r4zn0z3xm2dthop6a.htm&...

 

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.

--
Paige Miller
Tom
Super User Tom
Super User

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
fordcr2
Obsidian | Level 7

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?

fordcr2
Obsidian | Level 7

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. 

fordcr2
Obsidian | Level 7

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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 4286 views
  • 1 like
  • 4 in conversation