DATA Step, Macro, Functions and more

prxparse to find and replace a funky date in a string to SAS date expression

Accepted Solution Solved
Reply
Contributor
Posts: 54
Accepted Solution

prxparse to find and replace a funky date in a string to SAS date expression

I’m trying to change the funky date format coming in to a SAS date expression. '02FEB2012'd for example.

I am importing a string that may have one or more dates that look like this: 222201200:00:0DATE or 1222201200:00:0DATE in mmddYYYY00:00:0DATE format. The underlined part will always be the same (00:00:0DATE). The month and day are either single or double digits.

I’m trying the below but my skills lack my needs. Thanks for your help:

Data input;

  input str $ 1-92 want $ 93-174 ;

  cards;

This is string 1 where date eq. 11201200:00:0DATE                                                                  This is string 1 where date eq. '01JAN2012'd                                    

This is string 2 where dates between 22201200:00:0DATE and 121201200:00:0DATE end of strings This is string 2 where dates between '02FEB2012'd and '01DEC2012'd end of strings

;

run;

data want;

  set input;

  pnum=prxparse("/\d\d\d\d\d\d\d\d\d\:\d\d\:\d\w/o");

  x=1;

  do while (x gt 0);

    x = prxmatch(pnum,str);  

    if x then do;

      str1=catt(substr(str,1,x-1),

        put(input(substr(tranwrd(str,"00:00:0DATE",""),x,8), anydtdte12.),date9.),"'d",

        substr(str,x+9));

    end;

  end;

run;


Accepted Solutions
Solution
‎05-14-2012 11:06 AM
Trusted Advisor
Posts: 1,301

Re: prxparse to find and replace a funky date in a string to SAS date expression

ptions datestyle=mdy;

data foo;

input str $ 1-92 want $ 93-174;

start=1;

stop=length(str);

prx=prxparse('/\d{6,8}00:00:0DATE/');

call prxnext(prx,start,stop,str,pos,len);

do while (pos > 0);

  substr(str,pos,len)=cats("'",put(input(prxchange('s/(\d{1,2})(\d{1,2})(\d{4})00:00:0DATE/$1-$2-$3/',1,substr(str,pos,len)),anydtdte.),date9.),"'d");

  call prxnext(prx,start,stop,str,pos,len);

end;

str=compbl(str);

valid=(str=want);

keep str want valid;

cards;

This is string 1 where date eq. 11201200:00:0DATE                                                                  This is string 1 where date eq. '01JAN2012'd                                   

This is string 2 where dates between 22201200:00:0DATE and 121201200:00:0DATE end of strings This is string 2 where dates between '02FEB2012'd and '01DEC2012'd end of strings

;

run;

     

strwantvalid
This is string 1 where date eq. '01JAN2012'dThis is string 1 where date eq. '01JAN2012'd1
This is string 2 where dates between '02FEB2012'd and '01DEC2012'd end of stringsThis is string 2 where dates between '02FEB2012'd and '01DEC2012'd end of strings1

View solution in original post


All Replies
Solution
‎05-14-2012 11:06 AM
Trusted Advisor
Posts: 1,301

Re: prxparse to find and replace a funky date in a string to SAS date expression

ptions datestyle=mdy;

data foo;

input str $ 1-92 want $ 93-174;

start=1;

stop=length(str);

prx=prxparse('/\d{6,8}00:00:0DATE/');

call prxnext(prx,start,stop,str,pos,len);

do while (pos > 0);

  substr(str,pos,len)=cats("'",put(input(prxchange('s/(\d{1,2})(\d{1,2})(\d{4})00:00:0DATE/$1-$2-$3/',1,substr(str,pos,len)),anydtdte.),date9.),"'d");

  call prxnext(prx,start,stop,str,pos,len);

end;

str=compbl(str);

valid=(str=want);

keep str want valid;

cards;

This is string 1 where date eq. 11201200:00:0DATE                                                                  This is string 1 where date eq. '01JAN2012'd                                   

This is string 2 where dates between 22201200:00:0DATE and 121201200:00:0DATE end of strings This is string 2 where dates between '02FEB2012'd and '01DEC2012'd end of strings

;

run;

     

strwantvalid
This is string 1 where date eq. '01JAN2012'dThis is string 1 where date eq. '01JAN2012'd1
This is string 2 where dates between '02FEB2012'd and '01DEC2012'd end of stringsThis is string 2 where dates between '02FEB2012'd and '01DEC2012'd end of strings1
Contributor
Posts: 54

Re: prxparse to find and replace a funky date in a string to SAS date expression

Masterful! Thank you.

What does the first "call prxnext(prx,start,stop,str,pos,len);" do? I don't see where pos is defined so I'm not sure how that would work. Is it null?

Trusted Advisor
Posts: 1,301

Re: prxparse to find and replace a funky date in a string to SAS date expression

call prxnext(prx,start,stop,str,pos,len);


pos and len are defined in this subroutine call.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 346 views
  • 1 like
  • 2 in conversation