<?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: Check if an array contains a value in specific dimension in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Check-if-an-array-contains-a-value-in-specific-dimension/m-p/723360#M224453</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/150926"&gt;@kk13&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;I would like to find out if array (a1-a8) contains 1 or 0 using the date1-date8 array.&amp;nbsp; The check1 to check2 provides the dates that I need to look at.&amp;nbsp; For id=1, I need to look at if the dates are within check1&amp;lt;=dates{i}&amp;lt;=check2.&amp;nbsp; Therefore, date1 through date5 are within check1 and check2.&amp;nbsp; I need to check if a1 through a5 contains any 1.&amp;nbsp; If yes, then HAS=1.&amp;nbsp; If a1 through a5 are all zeros, then HAS=0.&amp;nbsp; If a1 through a5 are some zeros and some missing, then HAS is missing.&amp;nbsp; &lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can simplify the code by this approach:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Initialize has=0&lt;/LI&gt;
&lt;LI&gt;Loop through the eligible dates
&lt;OL&gt;
&lt;LI&gt;If the corresponding A has a 1, set HAS=1 and exit the loop&lt;/LI&gt;
&lt;LI&gt;If the corresponding A has a ., increment the counter NMISS&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;At the end of the loop, if HAS is not=1 but NMISS&amp;gt;0 then set HAS=.&lt;/LI&gt;
&lt;LI&gt;So HAS will stay at zero only if (1) no 1's were found, and (2) no missings were found - i.e. only 0's were encountered&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data z;
  input id a1-a8 date1-date8 check1 check2;
datalines;
1 0 0 1 1 . . . . 15 15 16 16 17 18 18 20 11 17
2 . . 1 1 0 1 1 1 14 16 17 18 19 21 22 24 8 18
3 . . . 1 1 1 1 1 14 15 17 17 18 19 21 22 16 23
4 0 0 0 0 0 1 1 1 13 15 18 18 21 21 23 23 10 18
;

/*I need to check if a1 through a5 
  -contains any 1.  If yes, then HAS=1.  
  If a1 through a5 are all zeros, then HAS=0.  
   If a1 through a5 are some zeros and some missing, then HAS is missing. */

 
data want (drop=i nmiss);
  set z;
  array a   {8} a1-a8;
  array dat {8} date1-date8;

  has=0;
  do i=1 to 8 while (dat{i}&amp;lt;=check2  and has=0);
    if dat{i}&amp;gt;=check1 then do;
      if a{i}=1 then has=1; else
      if a{i}=. then nmiss=sum(nmiss,1);      
    end;
  end;
  if has^=1 and nmiss&amp;gt;0 then has=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "while" expression does two things:
&lt;OL&gt;
&lt;LI&gt;It stops the loop when a date is reached that is greater than CHECK2&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Or it stops the loop once HAS=1&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;Note all dates less than CHECK1 are in the loop, but the internal DO group is not applied for them.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;&lt;STRONG&gt;IMPORTANT: This code assume DATE1 through DATE8 are in non-descending order.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
    <pubDate>Thu, 04 Mar 2021 04:57:22 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2021-03-04T04:57:22Z</dc:date>
    <item>
      <title>Check if an array contains a value in specific dimension</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-an-array-contains-a-value-in-specific-dimension/m-p/723334#M224439</link>
      <description>&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;I would like to find out if array (a1-a8) contains 1 or 0 using the date1-date8 array.&amp;nbsp; The check1 to check2 provides the dates that I need to look at.&amp;nbsp; For id=1, I need to look at if the dates are within check1&amp;lt;=dates{i}&amp;lt;=check2.&amp;nbsp; Therefore, date1 through date5 are within check1 and check2.&amp;nbsp; I need to check if a1 through a5 contains any 1.&amp;nbsp; If yes, then HAS=1.&amp;nbsp; If a1 through a5 are all zeros, then HAS=0.&amp;nbsp; If a1 through a5 are some zeros and some missing, then HAS is missing.&amp;nbsp; &lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; z;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; id a1-a8 date1-date8 check1 check2;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1 0 0 1 1 . . . . 15 15 16 16 17 18 18 20 11 17&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2 . . 1 1 0 1 1 1 14 16 17 18 19 21 22 24 8 18&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;3 . . . 1 1 1 1 1 14 15 17 17 18 19 21 22 16 23&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;4 0 0 0 0 0 1 1 1 13 15 18 18 21 21 23 23 10 18&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;FONT face="Courier New" size="2" color="#000080"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; want;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;input&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; id a1-a8 b1-b8 check1 check2 HAS;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;datalines&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;1 0 0 1 1 . . . . 15 15 16 16 17 18 18 20 11 17 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;2 . . 1 1 0 1 1 1 14 16 17 18 19 21 22 24 8 18 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;3 . . . 1 1 1 1 1 14 15 17 17 18 19 21 22 16 23 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;4 0 0 0 0 0 1 1 1 13 15 18 18 21 21 23 23 10 18 0&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;5 0 0 . . 1 1 1 1 10 11 11 13 13 17 17 18 9 11 .&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;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;I found jth dimension where dates is within check1 and check2.&amp;nbsp; I need to check if a1 through a jth contains any 0 or if a1 through a jth are all equal to 0.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;data&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; z1;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;set&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; z;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; a a1-a8;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;array&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; date date1-date8;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; i=&lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#008080"&gt;1&lt;/FONT&gt; &lt;FONT face="Courier New" size="2" color="#0000ff"&gt;to&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; dim(a);&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;if&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt; check1&amp;lt;=date{i}&amp;lt;=check2 &lt;/FONT&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;then&lt;/FONT&gt; &lt;FONT face="Courier New" size="2" color="#0000ff"&gt;do&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;j=i;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#0000ff"&gt;end&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;run&lt;/FONT&gt;&lt;FONT face="Courier New" size="2"&gt;;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 04 Mar 2021 00:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-an-array-contains-a-value-in-specific-dimension/m-p/723334#M224439</guid>
      <dc:creator>kk13</dc:creator>
      <dc:date>2021-03-04T00:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: Check if an array contains a value in specific dimension</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-an-array-contains-a-value-in-specific-dimension/m-p/723338#M224442</link>
      <description>&lt;P&gt;How is this code?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data z1(drop=i j:);
  set z;
  array a a1-a8;
  array date date1-date8;
  do i=1 to dim(a);
    if check1&amp;lt;=date{i} and j_start=. then do;
      j_start=i;
    end;
    if check1&amp;lt;=date{i}&amp;lt;=check2 then do;
      j_end=i;
    end;
  end;
  has=0;
  do i=j_start to j_end;
    if a{i}=1 then has=1;
    else has=has+a{i};
    if has=1 then leave;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 04 Mar 2021 01:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-an-array-contains-a-value-in-specific-dimension/m-p/723338#M224442</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-03-04T01:02:39Z</dc:date>
    </item>
    <item>
      <title>Re: Check if an array contains a value in specific dimension</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Check-if-an-array-contains-a-value-in-specific-dimension/m-p/723360#M224453</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/150926"&gt;@kk13&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;STRONG&gt;&lt;FONT face="Courier New" size="2" color="#000080"&gt;I would like to find out if array (a1-a8) contains 1 or 0 using the date1-date8 array.&amp;nbsp; The check1 to check2 provides the dates that I need to look at.&amp;nbsp; For id=1, I need to look at if the dates are within check1&amp;lt;=dates{i}&amp;lt;=check2.&amp;nbsp; Therefore, date1 through date5 are within check1 and check2.&amp;nbsp; I need to check if a1 through a5 contains any 1.&amp;nbsp; If yes, then HAS=1.&amp;nbsp; If a1 through a5 are all zeros, then HAS=0.&amp;nbsp; If a1 through a5 are some zeros and some missing, then HAS is missing.&amp;nbsp; &lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can simplify the code by this approach:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Initialize has=0&lt;/LI&gt;
&lt;LI&gt;Loop through the eligible dates
&lt;OL&gt;
&lt;LI&gt;If the corresponding A has a 1, set HAS=1 and exit the loop&lt;/LI&gt;
&lt;LI&gt;If the corresponding A has a ., increment the counter NMISS&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;At the end of the loop, if HAS is not=1 but NMISS&amp;gt;0 then set HAS=.&lt;/LI&gt;
&lt;LI&gt;So HAS will stay at zero only if (1) no 1's were found, and (2) no missings were found - i.e. only 0's were encountered&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data z;
  input id a1-a8 date1-date8 check1 check2;
datalines;
1 0 0 1 1 . . . . 15 15 16 16 17 18 18 20 11 17
2 . . 1 1 0 1 1 1 14 16 17 18 19 21 22 24 8 18
3 . . . 1 1 1 1 1 14 15 17 17 18 19 21 22 16 23
4 0 0 0 0 0 1 1 1 13 15 18 18 21 21 23 23 10 18
;

/*I need to check if a1 through a5 
  -contains any 1.  If yes, then HAS=1.  
  If a1 through a5 are all zeros, then HAS=0.  
   If a1 through a5 are some zeros and some missing, then HAS is missing. */

 
data want (drop=i nmiss);
  set z;
  array a   {8} a1-a8;
  array dat {8} date1-date8;

  has=0;
  do i=1 to 8 while (dat{i}&amp;lt;=check2  and has=0);
    if dat{i}&amp;gt;=check1 then do;
      if a{i}=1 then has=1; else
      if a{i}=. then nmiss=sum(nmiss,1);      
    end;
  end;
  if has^=1 and nmiss&amp;gt;0 then has=.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "while" expression does two things:
&lt;OL&gt;
&lt;LI&gt;It stops the loop when a date is reached that is greater than CHECK2&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Or it stops the loop once HAS=1&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;Note all dates less than CHECK1 are in the loop, but the internal DO group is not applied for them.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;EM&gt;&lt;STRONG&gt;IMPORTANT: This code assume DATE1 through DATE8 are in non-descending order.&lt;/STRONG&gt;&lt;/EM&gt;&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Thu, 04 Mar 2021 04:57:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Check-if-an-array-contains-a-value-in-specific-dimension/m-p/723360#M224453</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-04T04:57:22Z</dc:date>
    </item>
  </channel>
</rss>

