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

Hi, I'm trying to get a string between parenthesis when there are characters before and after the parenthesis.  The string looks like this: GGT (U/L)    1 - 5

 

I want the output to be this: U/L

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

Assuming there is only one left parenthesis and one right parenthesis (is that a good assumption?)

 

data want;
    set have;
    left=find(variablename,'(');
    right=find(variablename,')');
    desired_string=substr(variablename,left+1,right-left-1);
run;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Assuming there is only one left parenthesis and one right parenthesis (is that a good assumption?)

 

data want;
    set have;
    left=find(variablename,'(');
    right=find(variablename,')');
    desired_string=substr(variablename,left+1,right-left-1);
run;
--
Paige Miller
Astounding
PROC Star

As long as parentheses are not the first character, you can use:

inside = scan(variable, 2, ')(');

This selects the second set of characters within the string, using parentheses as delimiters.

ballardw
Super User

And easy enough to test if the ( is the first character:

data example;
   input string $10.;
datalines;
GGT (U/L)  
(start) of
;

data want;
   set example;
   if string =:'(' then want= scan(string,1,'()');
   else want=scan(string,2,'()');
run;

If you have not seen the =: that is basically a "begins with". So if the first character is ( then the first bit is grabbed, otherwise the second.

Ksharp
Super User
/*Assuming there is only one parenthesis*/
data example;
   input string $40.;
datalines;
GGT (U/L)    1 - 5
(start) of
;
data want;
 set example;
 pid=prxparse('/\(.*?\)/');
 call prxsubstr(pid,string,p,l);
 if p then want=compress(substr(string,p,l),'()');
 drop p l pid;
run;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 4 replies
  • 1831 views
  • 2 likes
  • 5 in conversation