BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Beto16
Obsidian | Level 7
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

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisNZ
Tourmaline | Level 20

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

9 REPLIES 9
PGStats
Opal | Level 21

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

 

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

PG
Beto16
Obsidian | Level 7

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 

 

ChrisNZ
Tourmaline | Level 20

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.

Beto16
Obsidian | Level 7
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
ChrisNZ
Tourmaline | Level 20

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

Beto16
Obsidian | Level 7
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
ChrisNZ
Tourmaline | Level 20

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

 

 

 

 

Beto16
Obsidian | Level 7
Hi Chris if I'm getting an Infile how do I make refer to all X's. For it to work ?
ChrisNZ
Tourmaline | Level 20

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

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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