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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.