Help using Base SAS procedures

How to use Prxparse to find two different words

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 80
Accepted Solution

How to use Prxparse to find two different words

[ Edited ]
Spoiler
 

Hi I have this snippet of code

 

Data have;
    Infile;
    Length bacode $25;
    N+1;
    Pid=prxparse ('/atmbarcode :\W*\w+/i');
    S=1;
    E=length (x);
    Call prxnext (pid,s,e,x,p,l);
    Do while (p>0);
        Bacode=substr (x.p.l);
        Output;
        Call prxnext (pid,s,e,x,p,l);
    End;
    Keep n x bacode;
Run;


What I need is to add a tracking number the tracking number starts with 1zt2. How can I add a 2nd search to the code to look for atmbarcode and 1zt2 as well? Thanks


Accepted Solutions
Solution
‎07-29-2016 09:27 AM
PROC Star
Posts: 1,555

Re: How to use Prxparse to find two different words

[ Edited ]

Like this:

 


data _null_; file "%sysfunc(pathname(WORK))\t.txt"; X='X. Bacode. N. Newvar ZT2487 '; put X; X='Blah,blah,blah, Atmbarcode :"TY272822. 2'; put X; X='Blah, blah,blah,blah, '; put X; X='1ZT2487A122111112,blah,blah, '; put X; run; data WANT; infile "%sysfunc(pathname(WORK))\t.txt" pad; input X $80.; PRX=prxparse('/(1ZT[A-Z\d]{14}) (?# capture 1ZT and 14 letters or digits) | (?# or) (?<=Atmbarcode\W{3}) (?# capture preceeded by Atmbarcode\W{3} ) (TY\d*) (?# capture TY and digits) /x'); call prxsubstr(PRX,X,POS,LEN); if POS then STR=substr(X,POS,LEN); putlog STR=; run;

 

STR=

STR=TY272822

STR=

STR=1ZT2487A122111112

 

 

All supported SAS regular expressions documented in:

 

High-Performance SAS Coding

 

https://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490

View solution in original post


All Replies
Respected Advisor
Posts: 4,640

Re: How to add an additional column

Can you give a few examples of variable x to work with?

 

Do you actually need "atmbarcode : " as part of the bacode string?

PG
Frequent Contributor
Posts: 80

Re: How to add an additional column

Hi I will examples  bacode is the name of column I create .. I'm looking for everything that starts with atmbarcode:  I now need to add the tracking number that is in another column and if starts with 1zt2.    ...so the first part of code works it brings in everything I need I need to add and statement to bring 1zt2 tracking number ..the location of the tracking numBer can vary from col 5 thru coL 300 .....I was hoping peral could assist 

 

PROC Star
Posts: 1,555

Re: How to add an additional column

Like this?

 

 


data have;
  Length bacode $25;
  X='lkdjf lsdjkl skljl atmbarcode :_SS jhjh  1zt2 : _5r kkj';
  Pid=prxparse ('/(atmbarcode|1zt2) :\W*\w+/i');
  S=1;
  E=length (x);
  Call prxnext (pid,s,e,x,p,l);
  Do while (p>0);
    Bacode=substr (x,p,l);
    putlog Bacode=;
    Call prxnext (pid,s,e,x,p,l);
  End;
Run;

 

bacode=atmbarcode :_SS

bacode=1zt2 : _5r

 

If not, please supply a couple of line of data and desired output variable values.

Frequent Contributor
Posts: 80

Re: How to use Prxparse to find two different words

It didn't work I can do it in steps first pulling atmbarcode which works than pulling for tracking number which is in column X example 1ZT28 an one more letter an 10 more digits all together 17 characters long .hi have this code

Data have;
Set have;
Newvar=('s/.+([A-Z](2)) *(\d+).*/$1$2/o',-1,'x'n);
Run;
My output
Is only 6 characters ZT2487 how can I tweak it to bring in all 18 characters? Thank you
PROC Star
Posts: 1,555

Re: How to use Prxparse to find two different words

Please feel free to ignore requests from @PGStats and myself to supply expected data and output, and still expect meaningful answers. Smiley Happy

Frequent Contributor
Posts: 80

Re: How to use Prxparse to find two different words

I thought I was providing you with all the pertaint information. .. since the atmbarcode works I fig it be easier to do it in steps since I have a code that brings 6 of the characters I thought you could show me how to tweak the code to bring the rest here is what I'm looking at right after I run the first part


X. Bacode. N. Newvar ZT2487
Blah,blah,blah, Atmbarcode :"TY272822. 2
Blah, blah,blah,blah,
1ZT2487A1221111111,blah,blah,

Sorry for the misunderstanding I thought I was helping by providing code an not example hopes that helps my thought process
PROC Star
Posts: 1,555

Re: How to use Prxparse to find two different words

[ Edited ]

Like this?

 

data HAVE;
  X='X. Bacode. N. Newvar ZT2487             '; output;
  X='Blah,blah,blah, Atmbarcode :"TY272822. 2'; output;
  X='Blah, blah,blah,blah,                   '; output;
  X='1ZT2487A122111112,blah,blah,            '; output;
run;
data WANT;
  set HAVE;
  PRX=prxparse('/(1ZT[A-Z\d]{14})|(?<=Atmbarcode\W{3})(TY\d*)/');
  call prxsubstr(PRX,X,POS,LEN);
  if POS then STR=substr(X,POS,LEN);
  putlog STR=;
run;

STR=

STR=TY272822

STR=

STR=1ZT2487A122111112

 

 

 

 

Frequent Contributor
Posts: 80

Re: How to use Prxparse to find two different words

Hi Chris if I'm getting an Infile how do I make refer to all X's. For it to work ?
Solution
‎07-29-2016 09:27 AM
PROC Star
Posts: 1,555

Re: How to use Prxparse to find two different words

[ Edited ]

Like this:

 


data _null_; file "%sysfunc(pathname(WORK))\t.txt"; X='X. Bacode. N. Newvar ZT2487 '; put X; X='Blah,blah,blah, Atmbarcode :"TY272822. 2'; put X; X='Blah, blah,blah,blah, '; put X; X='1ZT2487A122111112,blah,blah, '; put X; run; data WANT; infile "%sysfunc(pathname(WORK))\t.txt" pad; input X $80.; PRX=prxparse('/(1ZT[A-Z\d]{14}) (?# capture 1ZT and 14 letters or digits) | (?# or) (?<=Atmbarcode\W{3}) (?# capture preceeded by Atmbarcode\W{3} ) (TY\d*) (?# capture TY and digits) /x'); call prxsubstr(PRX,X,POS,LEN); if POS then STR=substr(X,POS,LEN); putlog STR=; run;

 

STR=

STR=TY272822

STR=

STR=1ZT2487A122111112

 

 

All supported SAS regular expressions documented in:

 

High-Performance SAS Coding

 

https://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490

☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 636 views
  • 1 like
  • 3 in conversation