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


I want to extract dates from a text string, most of them are at the same place,  But some have two dates  and may be in different places.   I can  use substrn function to get most of the dates. Any one has better way to do it? In addition, after substrn, they are in text format, how can I change to the date format still keeping the correct date value?

I attached the simplified sas file, my actual file imuch larger.

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

Anna,

I would use a regular expression.  The following will work and, hopefully, someone with more regex expertise can simplify it:

libname art "c:\art";

%let pattern=\d{1,2}\/\d{1,2}\/\d{2,4};

data want (drop=pos: len: prx:);

  set art.test;

  format date1 date2 mmddyy10.;

  prxid1=prxparse("/&pattern./o");

  prxid2=prxparse("/&pattern..*?(&pattern.)/o");

  if prxmatch(prxid1,INITIAL_CALL_DATA) then do;

    CALL PRXSUBSTR (prxid1, INITIAL_CALL_DATA, position , length);

    date1=input(substr(INITIAL_CALL_DATA, position , length),mmddyy10.);

    if prxmatch(prxid2,INITIAL_CALL_DATA) then do;

      CALL PRXSUBSTR (prxid1, substr(INITIAL_CALL_DATA, position+5),position2,length2);

      date2=input(substr(INITIAL_CALL_DATA, position+4+position2 , length2),mmddyy10.);

    end;

  end;

run;

View solution in original post

4 REPLIES 4
Reeza
Super User

It looks like you have multiple dates but the text is separated by ;.

You could try using the scan function instead to get the date parts.

I'd use two nested loops, one that first scans for ; and the find/index with substr to find the date parts. 

Anna_Guo
Calcite | Level 5

Reeza, Thank you. Can you be more specific? I cannot figure out.

art297
Opal | Level 21

Anna,

I would use a regular expression.  The following will work and, hopefully, someone with more regex expertise can simplify it:

libname art "c:\art";

%let pattern=\d{1,2}\/\d{1,2}\/\d{2,4};

data want (drop=pos: len: prx:);

  set art.test;

  format date1 date2 mmddyy10.;

  prxid1=prxparse("/&pattern./o");

  prxid2=prxparse("/&pattern..*?(&pattern.)/o");

  if prxmatch(prxid1,INITIAL_CALL_DATA) then do;

    CALL PRXSUBSTR (prxid1, INITIAL_CALL_DATA, position , length);

    date1=input(substr(INITIAL_CALL_DATA, position , length),mmddyy10.);

    if prxmatch(prxid2,INITIAL_CALL_DATA) then do;

      CALL PRXSUBSTR (prxid1, substr(INITIAL_CALL_DATA, position+5),position2,length2);

      date2=input(substr(INITIAL_CALL_DATA, position+4+position2 , length2),mmddyy10.);

    end;

  end;

run;

Anna_Guo
Calcite | Level 5

Arthur,

Thank you very much. It worked.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 4 replies
  • 3381 views
  • 3 likes
  • 3 in conversation