BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Noomen
Fluorite | Level 6

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.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

 

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

Is it anywhere in the text, or is it always in a particular location in the text?

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
Noomen
Fluorite | Level 6

It could be anywhere.

novinosrin
Tourmaline | Level 20

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
Noomen
Fluorite | Level 6

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;

 

novinosrin
Tourmaline | Level 20

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

 

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 5 replies
  • 893 views
  • 1 like
  • 3 in conversation