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.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 4335 views
  • 3 likes
  • 3 in conversation