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
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;
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;
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.
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.
/*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;
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.