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

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

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;
PeterClemmensen
Tourmaline | Level 20

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;

sas-innovate-2024.png

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.

 

Register now!

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
  • 2 replies
  • 514 views
  • 4 likes
  • 3 in conversation