Hi everyone!
I would like to extract all the strings with a specific format for example "ABCxxx_x".
The 'x' symbol refers to digit numbers (from 0 to 9).
For example, I want to get the string "ABC123_3" or "ABC598_4".
Could anyone help me?
Thank you in advance!
Here's an example using PRXMATCH
I made some assumptions, but the example should help
data have ;
infile cards ;
input myText :$12. ;
cards;
ABC123_4
DEF567_8
not_me
or_me
or_123
;
data want ;
retain patternID;
if _n_=1 then do ;
pattern="/[A-Z]{3}[0-9]{3}_[0-9]/";
patternID=prxparse(pattern);
end ;
set have ;
position=prxmatch(patternID, myText);
put mytext= @25 position= ;
run ;
So you want all strings that begin with "ABC"? Or all strings that begin with any three letters followed by numbers and/or symbols? Or can strings that begin with 2 characters or 4 characters also be acceptable?
Don't make us generalize from a single example, that is impossible.
Please explain exactly what you are looking for. Give examples of what you want and what you don't want.
So you only need to adapt the pattern given by @AMSAS :
pattern="/ABC[0-9]{3}_[0-9]/";
We need to know how the strings look it and what you expect as result., so please provide data in usable form and show what you want.
Here's an example using PRXMATCH
I made some assumptions, but the example should help
data have ;
infile cards ;
input myText :$12. ;
cards;
ABC123_4
DEF567_8
not_me
or_me
or_123
;
data want ;
retain patternID;
if _n_=1 then do ;
pattern="/[A-Z]{3}[0-9]{3}_[0-9]/";
patternID=prxparse(pattern);
end ;
set have ;
position=prxmatch(patternID, myText);
put mytext= @25 position= ;
run ;
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.