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

Hello everyone,

 

I need to extract datetime from a character string; I am not sure how to go about it.

 

For example the field for the variable "comment" is:

"Scheduled Appt. Study: Healthy Brain Study. Visit: Consultation. Date: 11/30/2017 11:00 AM"

 

Is there a function to keep only datetime for all observations?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Are you after this:

or please correct my understanding

data have;
input var $100.;
datalines;
Scheduled Appt. Study: Healthy Brain Study. Visit: Consultation. Date: 11/30/2017 11:00 AM
;

data want;
set have;
just_time=substr(var,index(var,'Date')+5);
run;

View solution in original post

8 REPLIES 8
mglogan
Obsidian | Level 7
Yes! "Date:" would be it.
novinosrin
Tourmaline | Level 20

Are you after this:

or please correct my understanding

data have;
input var $100.;
datalines;
Scheduled Appt. Study: Healthy Brain Study. Visit: Consultation. Date: 11/30/2017 11:00 AM
;

data want;
set have;
just_time=substr(var,index(var,'Date')+5);
run;

mglogan
Obsidian | Level 7
Thank you! This is exactly what I needed.
lopezr
Obsidian | Level 7

With a slight modification of @novinosrin's code you can create a numeric date/time variable.

data want;
  set have;
  just_time=input(substr(var,index(var,'Date')+5), mdyampm25.);
  format just_time mdyampm25.;
run;
mglogan
Obsidian | Level 7
Thank you! This worked great.
kiranv_
Rhodochrosite | Level 12

Just another way. it works only when your date time is in pattern you have shown

data have;
input comment $150.;
datalines;
Scheduled Appt. Study: Healthy Brain Study. Visit: Consultation. Date: 11/30/2017 11:00 AM
Scheduled Appt. Study: Healthy Brain Study. Visit: Consultation. Date: 11/30/2017 12:00 PM looks good
;


data want;
   set have;
   name = prxchange('s/(.*?)(\d{1,2}\/\d{2}\/\d{4} \d{2}:\d{2}\s[A-Za-z]{2})(.*)/$2/', -1, comment);
run;
ballardw
Super User

If you are reading this from a text file you might find it easier just to modify the original code reading it:

data have;
   informat ddate mmddyy10.;
   format   ddate mmddyy10.;
   input  @"Date: " Ddate;
datalines;
Scheduled Appt. Study: Healthy Brain Study. Visit: Consultation. Date: 11/30/2017 11:00 AM
;
run;

The @ used in this manner in an input statement finds the sting in quotes and starts inputting at that point. If this is part of a longer input statement you may need to resent the current column position by @1 @"Date: " to avoid attempting to read past the end of the line.

 

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
  • 8 replies
  • 4958 views
  • 6 likes
  • 6 in conversation