<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Find position of end/matching Bracket in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791294#M253429</link>
    <description>Hi ballardw,&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Thank you</description>
    <pubDate>Thu, 20 Jan 2022 21:38:17 GMT</pubDate>
    <dc:creator>kyle1190</dc:creator>
    <dc:date>2022-01-20T21:38:17Z</dc:date>
    <item>
      <title>Find position of end/matching Bracket</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791251#M253401</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;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?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data have;
length HAVE $50.;
	input HAVE $;
	datalines;
{}
{{}{}{}{}{}}
{{}{}{}{}{{}}}{}
{{}{{{}}}{}{}{{}}}{}{{}}
;run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here I have manually bold the matching end bracket of interest for clarity:&lt;/P&gt;&lt;P&gt;{&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;{{}{}{}{}{}&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;{{}{}{}{}{{}}&lt;STRONG&gt;}&lt;/STRONG&gt;{}&lt;/P&gt;&lt;P&gt;{{}{{{}}}{}{}{{}}&lt;STRONG&gt;}&lt;/STRONG&gt;{}{{}}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2;&lt;/P&gt;&lt;P&gt;12;&lt;/P&gt;&lt;P&gt;14;&lt;/P&gt;&lt;P&gt;18;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2022 19:59:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791251#M253401</guid>
      <dc:creator>kyle1190</dc:creator>
      <dc:date>2022-01-20T19:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of end/matching Bracket</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791253#M253402</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/413506"&gt;@kyle1190&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;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?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;data have;
length HAVE $50.;
	input HAVE $;
	datalines;
{}
{{}{}{}{}{}}
{{}{}{}{}{{}}}{}
{{}{{{}}}{}{}{{}}}{}{{}}
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here I have manually bold the matching end bracket of interest for clarity:&lt;/P&gt;
&lt;P&gt;{&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;{{}{}{}{}{}&lt;STRONG&gt;}&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;{{}{}{}{}{{}}&lt;STRONG&gt;}&lt;/STRONG&gt;{}&lt;/P&gt;
&lt;P&gt;{{}{{{}}}{}{}{{}}&lt;STRONG&gt;}&lt;/STRONG&gt;{}{{}}&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2;&lt;/P&gt;
&lt;P&gt;12;&lt;/P&gt;
&lt;P&gt;14;&lt;/P&gt;
&lt;P&gt;18;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2022 20:09:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791253#M253402</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-20T20:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of end/matching Bracket</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791294#M253429</link>
      <description>Hi ballardw,&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Thank you</description>
      <pubDate>Thu, 20 Jan 2022 21:38:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791294#M253429</guid>
      <dc:creator>kyle1190</dc:creator>
      <dc:date>2022-01-20T21:38:17Z</dc:date>
    </item>
    <item>
      <title>Re: Find position of end/matching Bracket</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791310#M253441</link>
      <description>&lt;P&gt;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.&lt;/P&gt;
&lt;PRE&gt;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;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/413506"&gt;@kyle1190&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi ballardw,&lt;BR /&gt;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.&lt;BR /&gt;&lt;BR /&gt;Thank you&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2022 22:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-position-of-end-matching-Bracket/m-p/791310#M253441</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-20T22:04:57Z</dc:date>
    </item>
  </channel>
</rss>

