SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kyle1190
Calcite | Level 5

Hi All,

I am trying to come up with a way to scan strings and locate the matching end bracket to the first parent bracket. The difficult part is that there may be many levels of nested brackets within and/or adjacent to the bracket pair of interest. I am not as familiar with perl regular expressions but after doing some searching seems like it may be one potential path to take? Or some tedious do-looping? 

 

Note, the first character of the string will always be the parent bracket of interest. Additionally there will always be an even number of { and }. For example, have:

 

data have;
length HAVE $50.;
	input HAVE $;
	datalines;
{}
{{}{}{}{}{}}
{{}{}{}{}{{}}}{}
{{}{{{}}}{}{}{{}}}{}{{}}
;run;

Here I have manually bold the matching end bracket of interest for clarity:

{}

{{}{}{}{}{}}

{{}{}{}{}{{}}}{}

{{}{{{}}}{}{}{{}}}{}{{}}

 

 

Want:

 

2;

12;

14;

18;

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This seems to find the matches for your example data. You could likely drop the i,open and close variables after testing. If you have not seen it before the LEAVE instruction exits a loop on execution so this quits when the match is found.

data want;
   set have;
   /* assumes we are matching the FIRST { character*/
   do i=1 to length(have);
      if substr(have,i,1)='{' then open= sum(open,1);
      if substr(have,i,1)='}' then close=sum(close,1);
      if open=close then do;
         matchposition=i;
         leave;
      end;
   end;
run;

@kyle1190 wrote:
Hi ballardw,
Thanks for the quick response. Not JSON, actually in this case I am trying to parse some RTF files read in as text lines in order to make some minor adjustments and then recreate the RTF files. I would be okay with somewhat lengthy code, if necessary.

Thank you

 

View solution in original post

3 REPLIES 3
ballardw
Super User

If you are trying to read data from a file it may help to provide some information about that. This sounds like it may possibly be JSON, in which case you might want to use a LIBNAME with the JSON engine to read the data instead of writing a bunch of, quite possibly complex and fragile, code.

 


@kyle1190 wrote:

Hi All,

I am trying to come up with a way to scan strings and locate the matching end bracket to the first parent bracket. The difficult part is that there may be many levels of nested brackets within and/or adjacent to the bracket pair of interest. I am not as familiar with perl regular expressions but after doing some searching seems like it may be one potential path to take? Or some tedious do-looping? 

 

Note, the first character of the string will always be the parent bracket of interest. Additionally there will always be an even number of { and }. For example, have:

 

data have;
length HAVE $50.;
	input HAVE $;
	datalines;
{}
{{}{}{}{}{}}
{{}{}{}{}{{}}}{}
{{}{{{}}}{}{}{{}}}{}{{}}
;run;

Here I have manually bold the matching end bracket of interest for clarity:

{}

{{}{}{}{}{}}

{{}{}{}{}{{}}}{}

{{}{{{}}}{}{}{{}}}{}{{}}

 

 

Want:

 

2;

12;

14;

18;

 

Thank you


 

kyle1190
Calcite | Level 5
Hi ballardw,
Thanks for the quick response. Not JSON, actually in this case I am trying to parse some RTF files read in as text lines in order to make some minor adjustments and then recreate the RTF files. I would be okay with somewhat lengthy code, if necessary.

Thank you
ballardw
Super User

This seems to find the matches for your example data. You could likely drop the i,open and close variables after testing. If you have not seen it before the LEAVE instruction exits a loop on execution so this quits when the match is found.

data want;
   set have;
   /* assumes we are matching the FIRST { character*/
   do i=1 to length(have);
      if substr(have,i,1)='{' then open= sum(open,1);
      if substr(have,i,1)='}' then close=sum(close,1);
      if open=close then do;
         matchposition=i;
         leave;
      end;
   end;
run;

@kyle1190 wrote:
Hi ballardw,
Thanks for the quick response. Not JSON, actually in this case I am trying to parse some RTF files read in as text lines in order to make some minor adjustments and then recreate the RTF files. I would be okay with somewhat lengthy code, if necessary.

Thank you

 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1545 views
  • 3 likes
  • 2 in conversation