<?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: How many consecutive zeros in a variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-many-consecutive-zeros-in-a-variable/m-p/694282#M211726</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290100"&gt;@zishaq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a character variable called employment_flag of length 12 which either contains the values 1 or 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;010000000010&lt;/P&gt;
&lt;P&gt;011100100001&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to find out the &lt;STRONG&gt;longest string of consecutive zeros&lt;/STRONG&gt; &lt;STRONG&gt;between the first and last 1&lt;/STRONG&gt; for each value in this variable. For example, the answer for row 1 should be 8 and row 2 should be 4. Note that in example 2 we have a string of 2 zeros and 4 zeros but I'm interested in the longest string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is much appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;data example;
   input x :$12.;
   longest=0;
   do i=1 to countw(x,'1');
      longest = max(longest, length(scan(x,i,'1')));
   end;
   drop i;
datalines;
010000000010
011100100001
;&lt;/PRE&gt;
&lt;P&gt;The above code treats the 1 as delimiter for both the countw and scan functions to break the string up into "words" consisting of only 0 and uses the length function to return the number of characters.&lt;/P&gt;</description>
    <pubDate>Mon, 26 Oct 2020 16:10:53 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2020-10-26T16:10:53Z</dc:date>
    <item>
      <title>How many consecutive zeros in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-consecutive-zeros-in-a-variable/m-p/694279#M211725</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I have a character variable called employment_flag of length 12 which either contains the values 1 or 0.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;010000000010&lt;/P&gt;&lt;P&gt;011100100001&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to find out the &lt;STRONG&gt;longest string of consecutive zeros&lt;/STRONG&gt; &lt;STRONG&gt;between the first and last 1&lt;/STRONG&gt; for each value in this variable. For example, the answer for row 1 should be 8 and row 2 should be 4. Note that in example 2 we have a string of 2 zeros and 4 zeros but I'm interested in the longest string.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 15:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-consecutive-zeros-in-a-variable/m-p/694279#M211725</guid>
      <dc:creator>zishaq</dc:creator>
      <dc:date>2020-10-26T15:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: How many consecutive zeros in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-consecutive-zeros-in-a-variable/m-p/694282#M211726</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290100"&gt;@zishaq&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have a character variable called employment_flag of length 12 which either contains the values 1 or 0.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;010000000010&lt;/P&gt;
&lt;P&gt;011100100001&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to find out the &lt;STRONG&gt;longest string of consecutive zeros&lt;/STRONG&gt; &lt;STRONG&gt;between the first and last 1&lt;/STRONG&gt; for each value in this variable. For example, the answer for row 1 should be 8 and row 2 should be 4. Note that in example 2 we have a string of 2 zeros and 4 zeros but I'm interested in the longest string.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help is much appreciated!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;One way:&lt;/P&gt;
&lt;PRE&gt;data example;
   input x :$12.;
   longest=0;
   do i=1 to countw(x,'1');
      longest = max(longest, length(scan(x,i,'1')));
   end;
   drop i;
datalines;
010000000010
011100100001
;&lt;/PRE&gt;
&lt;P&gt;The above code treats the 1 as delimiter for both the countw and scan functions to break the string up into "words" consisting of only 0 and uses the length function to return the number of characters.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 16:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-consecutive-zeros-in-a-variable/m-p/694282#M211726</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-26T16:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: How many consecutive zeros in a variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-many-consecutive-zeros-in-a-variable/m-p/694297#M211730</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/290100"&gt;@zishaq&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's another (but very similar) suggestion in view of the condition "&lt;STRONG&gt;between the first and last 1&lt;/STRONG&gt;":&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input c $12.;
cards;
010000000010
011100100001
;

data want;
set have;
do _n_=2 to countw(c,'1','m')-1;
  m=max(m,lengthn(scan(c,_n_,'1','m')));
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In case of less than two 1s in the string the above solution would (deliberately) return a missing value.&lt;/P&gt;</description>
      <pubDate>Mon, 26 Oct 2020 16:49:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-many-consecutive-zeros-in-a-variable/m-p/694297#M211730</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-10-26T16:49:20Z</dc:date>
    </item>
  </channel>
</rss>

