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

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 1182 views
  • 2 likes
  • 5 in conversation