Hi I would like to remove brackets plus any text inside, for example:
have:
VAR1
text(where=(upcase(num)="A"))
text text (in=a)
text (in=a) text2 (where=(upcase(strip(var1))))
want:
VAR1
text
text text
text text2
thanks for your help
Just for fun
data have;
input var1 $ 1 - 50;
datalines;
text(where=(upcase(num)="A"))
text text (in=a)
text (in=a) text2 (where=(upcase(strip(var1))))
;
data want;
set have;
do i = 1 to length(var1);
if char(var1, i) = "(" then do;
c = 0;
do j = i to length(var1);
if char(var1, j) = "(" then c ++ 1;
if char(var1, j) = ")" then c +- 1;
substr(var1, j, 1) = "";
if c = 0 then leave;
end;
end;
end;
var1 = compbl(var1);
run;
Hi @kalbo,
If those "brackets" are only balanced parentheses, try a recursive approach like this:
data have;
input var1 $80.;
datalines;
text(where=(upcase(num)="A"))
text text (in=a)
text (in=a) text2 (where=(upcase(strip(var1))))
;
data want(drop=_:);
set have;
do until(var1=_var1);
_var1=var1;
var1=compbl(prxchange('s/\([^()]*\)//',-1,var1));
end;
run;
Just for fun
data have;
input var1 $ 1 - 50;
datalines;
text(where=(upcase(num)="A"))
text text (in=a)
text (in=a) text2 (where=(upcase(strip(var1))))
;
data want;
set have;
do i = 1 to length(var1);
if char(var1, i) = "(" then do;
c = 0;
do j = i to length(var1);
if char(var1, j) = "(" then c ++ 1;
if char(var1, j) = ")" then c +- 1;
substr(var1, j, 1) = "";
if c = 0 then leave;
end;
end;
end;
var1 = compbl(var1);
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.