BookmarkSubscribeRSS Feed
surajmetha55
Fluorite | Level 6

Dear All,

             I want to extract all dates from  term variable:

 

The data in term vriables is:

 

Days of 08 April 19

count on 10 May 2019, 05 May  2018, 08-Jan-2005

apply i.e. 08 April 19 and 10-Jan-2005

 

I want ro extract all dates from above variable, in one or multiple vriables using regex or perl regular expression 

 

I tried the following program but I am geting only last date

 

data cd.new;
    set cd.Date_ex;
    dsdate=prxchange('s/.*(\d{2}(\s+|-)\w+(\s+|-)\d{2,4}).*/\1/', 1, term);
run;

Output:

08 April 19

08-Jan-2005

10-Jan-2005

 

2 REPLIES 2
Patrick
Opal | Level 21

May be try and change the second last parameter from 1 to -1 so that the RegEx gets applied as many times as possible.

dsdate=prxchange('s/.*(\d{2}(\s+|-)\w+(\s+|-)\d{2,4}).*/\1/', -1, term);

 

Alternatively use a RegEx to just match the date patterns in the string and then implement a loop to extract all matching occurrences within the string using call prxnext() 

https://go.documentation.sas.com/?docsetId=lefunctionsref&docsetTarget=n1obc9u7z3225mn1npwnassehff0.... 

Ksharp
Super User

It is really uneasy . This could give you a start.

 

data have;
input have $80.;
cards;
Days of 08 April 19
count on 10 May 2019, 05 May  2018, 08-Jan-2005
apply i.e. 08 April 19 and 10-Jan-2005
;

data want;
 set have;
 pid=prxparse('/\b\d+\W+[a-z]+\W+\d+\b/i');
 s=1;
 e=length(have);
 call prxnext(pid,s,e,have,p,l);
 do while(p>0);
  want=substr(have,p,l);
  output;
  call prxnext(pid,s,e,have,p,l);
 end;
drop s e p l pid;
run;

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

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 1937 views
  • 0 likes
  • 3 in conversation