<?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 can I adapt my code to include only gaps that have a row at any point before or after it? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-adapt-my-code-to-include-only-gaps-that-have-a-row-at/m-p/904410#M357328</link>
    <description>For me, there is still a bit of guesswork as to what you need.  So here is an approach for you to consider.  &lt;BR /&gt;&lt;BR /&gt;Start  at the beginning of the array and find the first 1.  For example, if the first 1 is in YEAR99, the variable First1 would be 3.&lt;BR /&gt;Then count backwards from the end of the array.  For example, if the first 1 when counting backwards is in YEAR06, then Last1 would be 10.&lt;BR /&gt;Finally, count the number of 0 values between First1 and Last1.&lt;BR /&gt;Does this approach come close to doing what you need?</description>
    <pubDate>Thu, 23 Nov 2023 17:58:31 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2023-11-23T17:58:31Z</dc:date>
    <item>
      <title>How can I adapt my code to include only gaps that have a row at any point before or after it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-adapt-my-code-to-include-only-gaps-that-have-a-row-at/m-p/904405#M357324</link>
      <description>&lt;P&gt;Hi there.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I've got stuck with my SAS code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My aim: To find the average gap of a row (but only a gap where there is either a year before or after the gap) So if someone joins for 2 years, leaves then comes back 2 years later it will count a 2 year gap. However if someone joins and stays for 3 year and then leaves they should not be counted if that's all they did.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've managed to get to a point the code will either include to much or not enough. I can make it either include all gaps (even if they have never been in before, so essentially just all 0's) or only when there's a specified amount of years either side of it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data my_data;&lt;BR /&gt;input KEY YEAR97 YEAR98 YEAR99 YEAR00 YEAR01 YEAR02 YEAR03 YEAR04 YEAR05 YEAR06 YEAR07 YEAR08 YEAR09;&lt;BR /&gt;datalines;&lt;BR /&gt;1 0 0 1 0 0 1 0 1 0 0 1 0 0&lt;BR /&gt;2 1 1 0 1 1 0 1 0 1 1 0 1 1&lt;BR /&gt;3 0 0 0 0 1 0 0 0 1 0 0 1 0&lt;BR /&gt;4 1 1 1 1 1 1 1 1 1 1 1 1 1&lt;BR /&gt;;&lt;/P&gt;&lt;P&gt;data gaps_in_year;&lt;BR /&gt;set my_data;&lt;BR /&gt;array year_array YEAR97 YEAR98 YEAR99 YEAR00 YEAR01 YEAR02 YEAR03 YEAR04 YEAR05 YEAR06 YEAR07 YEAR08 YEAR09;&lt;BR /&gt;gap_counter = 0;&lt;BR /&gt;total_gap_length = 0;&lt;/P&gt;&lt;P&gt;do i = 2 to dim(year_array)-1;&lt;BR /&gt;if year_array[i-1] = 1 and year_array[i] = 0 and year_array[i+1] = 1 then do;&lt;BR /&gt;gap_counter + 1;&lt;BR /&gt;end;&lt;BR /&gt;else if gap_counter &amp;gt; 0 then do;&lt;BR /&gt;total_gap_length + gap_counter;&lt;BR /&gt;gap_counter = 0;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;/* Calculate average gap length for the current row */&lt;BR /&gt;if gap_counter &amp;gt; 0 then do;&lt;BR /&gt;total_gap_length + gap_counter;&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;drop i ;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Nov 2023 16:52:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-adapt-my-code-to-include-only-gaps-that-have-a-row-at/m-p/904405#M357324</guid>
      <dc:creator>Coder_12</dc:creator>
      <dc:date>2023-11-23T16:52:16Z</dc:date>
    </item>
    <item>
      <title>Re: How can I adapt my code to include only gaps that have a row at any point before or after it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-adapt-my-code-to-include-only-gaps-that-have-a-row-at/m-p/904410#M357328</link>
      <description>For me, there is still a bit of guesswork as to what you need.  So here is an approach for you to consider.  &lt;BR /&gt;&lt;BR /&gt;Start  at the beginning of the array and find the first 1.  For example, if the first 1 is in YEAR99, the variable First1 would be 3.&lt;BR /&gt;Then count backwards from the end of the array.  For example, if the first 1 when counting backwards is in YEAR06, then Last1 would be 10.&lt;BR /&gt;Finally, count the number of 0 values between First1 and Last1.&lt;BR /&gt;Does this approach come close to doing what you need?</description>
      <pubDate>Thu, 23 Nov 2023 17:58:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-adapt-my-code-to-include-only-gaps-that-have-a-row-at/m-p/904410#M357328</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-11-23T17:58:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I adapt my code to include only gaps that have a row at any point before or after it?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-adapt-my-code-to-include-only-gaps-that-have-a-row-at/m-p/904433#M357335</link>
      <description>&lt;P&gt;You can&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Concatenate all the 1's and 0's in the YEAR vars into a single character variable (_strng).&lt;/LI&gt;
&lt;LI&gt;Trim leading and trailing 0's from _strng, so that all remaining zeroes must be between 1's.&lt;/LI&gt;
&lt;LI&gt;Then&amp;nbsp;
&lt;OL&gt;
&lt;LI&gt;Treating 1's as word separators, count the number of "words" (NGAPS)&lt;/LI&gt;
&lt;LI&gt;Get total number of zeroes (compress out the 1's, get the length --&amp;gt;TOTAL_GAP_LENGTH).&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;Calculate the average gap length.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data my_data;
input KEY YEAR97 YEAR98 YEAR99 YEAR00 YEAR01 YEAR02 YEAR03 YEAR04 YEAR05 YEAR06 YEAR07 YEAR08 YEAR09;
datalines;
1 0 0 1 0 0 1 0 1 0 0 1 0 0
2 1 1 0 1 1 0 1 0 1 1 0 1 1
3 0 0 0 0 1 0 0 0 1 0 0 1 0
4 1 1 1 1 1 1 1 1 1 1 1 1 1
;


data want (drop=_:);
  set my_data;
  length _strng  $14;     *Assign a length at least one byte longer than N of YEAR variables *;

  _strng=cats(of year:);

  _strng=substr(_strng,findc(_strng,'1'));   *trim leading zeroes;
  _last1=findc(_strng,'1',-14);              *Find RIGHTMOST '1', note negative 3rd parameter;
  _strng=substr(_strng,1,_last1);            *trim trailing zeroes*;

  ngaps=countw(_strng,'1')-1;
  total_gap_length=lengthn(compress(_strng,'1'));
  if ngaps&amp;gt;0 then average_gap_length=total_gap_length/ngaps;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Nov 2023 18:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-adapt-my-code-to-include-only-gaps-that-have-a-row-at/m-p/904433#M357335</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-11-24T18:35:26Z</dc:date>
    </item>
  </channel>
</rss>

