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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1284 views
  • 0 likes
  • 3 in conversation