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
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:
https://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490
Can you give a few examples of variable x to work with?
Do you actually need "atmbarcode : " as part of the bacode string?
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
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.
Please feel free to ignore requests from @PGStats and myself to supply expected data and output, and still expect meaningful answers. 🙂
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
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:
https://www.amazon.com/High-Performance-SAS-Coding-Christian-Graffeuille/dp/1512397490
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.