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: Call for Content

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!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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