Hi all,
I want to extract (from a variable called textvar) the following expression : the two letters UN followed by 4 numbers. This means from 'UN0001' to "UN9999".
I need help with the prx because I found examples are about numbers or letters but not both !
data want ; set have ;
num = prxchange(' ', textvar);
newvar=num;
run;
Thanks for your help.
Hi @Noomen No worries at all. Assuming you have posted a representative sample for us, the solution seems rather simple as the following. Please see if this helps.
DATA have;
texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)"; output;
texto="While in transit, product UN1258 spilled"; output; run;
data want;
set have;
product=prxchange('s/(.*)(UN\d{4})(.*)/$2/i', -1, texto);
run;
proc print noobs;run;
texto | product |
---|---|
DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED) | UN3098 |
While in transit, product UN1258 spilled | UN1258 |
Is it anywhere in the text, or is it always in a particular location in the text?
It could be anywhere.
data have;
text='UN0001';
output;
text="UN9999";
output;
run;
/*With UN as prefix, so hardcoded the letters UN followed by digits*/
data want;
set have;
num=prxchange('s/UN(\d{4})/$1/', -1, text);
run;
/*or*/
/*any 2 letters as prefix followed by numbers*/
data want;
set have;
num=prxchange('s/[a-z]{2}(\d{4})/$1/i', -1, text);
run;
text | num |
---|---|
UN0001 | 0001 |
UN9999 | 9999 |
Hello, I apologize, I was busy so I couldn't give you feedback sooner.
Your solution is close but It is not exactly what I am looking for.
The product ID written as UNXXXX has to be copied form the variable texto to product, as shown below.
Variable texto stays the same.
Thank you all.
DATA have;
texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)"; output;
texto="While in transit, product UN1258 spilled"; output; run;
DATA want ;
texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDE)"; product="UN3098"; output;
texto="While in transit, product UN1258 spilled"; product="UN1258";output; run;
Hi @Noomen No worries at all. Assuming you have posted a representative sample for us, the solution seems rather simple as the following. Please see if this helps.
DATA have;
texto="DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED)"; output;
texto="While in transit, product UN1258 spilled"; output; run;
data want;
set have;
product=prxchange('s/(.*)(UN\d{4})(.*)/$2/i', -1, texto);
run;
proc print noobs;run;
texto | product |
---|---|
DURING UNLOADING, IT WAS FOUND THAT A TRUCK with UN3098 (HYDROGEN PEROXIDESPILLED) | UN3098 |
While in transit, product UN1258 spilled | UN1258 |
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.