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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
AMSAS
SAS Super FREQ

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 ;

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Chrisas
Fluorite | Level 6
Thank you for your response. I want the first three letters to begin with "ABC" then the next three characters to be digit number from 0-9, then underscore and then one digit number from 0-9.

For example: I want ABC123_3 or ABC564_9.
andreas_lds
Jade | Level 19

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.

AMSAS
SAS Super FREQ

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 ;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1856 views
  • 1 like
  • 5 in conversation