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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.