<?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 do I select the kth nonmissing variable or the first nonzero variable in an array? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482542#M286871</link>
    <description>&lt;P&gt;Use an ARRAY and a DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input 
ID  $ Y1994  Y1996  Y1998  Y2000 Y2002 Y2004 ;
cards;
01      .            .            .           .          12        12
02      .            .            .           .            .           . 
03      .            14         .            .           .           .
04      0           15         15         .           .         15
05       .            0          17         16         .          .
;

data want ;
 set have ;
 array y y:;
 do i=1 to dim(y) until (a not in (.,0)) ;
   a=y(i);
 end;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 373px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22112i6E981B9C62BA4C96/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 30 Jul 2018 16:53:10 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-07-30T16:53:10Z</dc:date>
    <item>
      <title>How do I select the kth nonmissing variable or the first nonzero variable in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482524#M286868</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have code that selects the first non-missing variable from an array of variables:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA want;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;set have;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;array t(*) YA: ;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;a=coalesce(of t(*));&amp;nbsp;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How do I adjust this code to instead select the first non-zero (and nonmissing)&amp;nbsp;variable in the array?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is an example of my data:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Y1994&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;Y1996&amp;nbsp;&amp;nbsp;Y1998&amp;nbsp; Y2000&amp;nbsp;Y2002&amp;nbsp;Y2004&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;01&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;02&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;03&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;04&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;05&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 17&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I would like:&lt;/P&gt;&lt;P&gt;ID&amp;nbsp; &amp;nbsp;Y1994&amp;nbsp;&amp;nbsp;&lt;SPAN&gt;Y1996&amp;nbsp;&amp;nbsp;Y1998&amp;nbsp; Y2000&amp;nbsp;Y2002&amp;nbsp;Y2004&amp;nbsp; &amp;nbsp; a&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;01&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 12&amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;02&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;03&amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 14&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; 14&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;04&amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15&amp;nbsp; &amp;nbsp; &amp;nbsp;15&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;05&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 0&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 17&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&amp;nbsp; &amp;nbsp; &amp;nbsp; 17&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;AMIHIC&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 16:45:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482524#M286868</guid>
      <dc:creator>Amihic</dc:creator>
      <dc:date>2018-07-30T16:45:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select the kth nonmissing variable or the first nonzero variable in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482528#M286869</link>
      <description>&lt;P&gt;Correct, coalesce takes the first non-missing, so would not work.&amp;nbsp; You could do it a number of ways depending on what your data looks like - which you have not provided:&lt;/P&gt;
&lt;P&gt;You could concatenate all the array into a string, then compress out missings.&lt;/P&gt;
&lt;P&gt;You could loop over the array and find the Nth non missing, something like:&lt;/P&gt;
&lt;PRE&gt;%let n=4;
data want;
  set have;
  array t{*} yafs:;
  n=0;
  do i=1 to dim(t);
    if t{i} ne . then n=sum(n,1);
    if n=&amp;amp;n. then result=n;
  end;
run;
&lt;/PRE&gt;</description>
      <pubDate>Mon, 30 Jul 2018 16:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482528#M286869</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-07-30T16:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select the kth nonmissing variable or the first nonzero variable in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482538#M286870</link>
      <description>&lt;P&gt;Hi RW9,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I edited my original post to include a sample of my data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;AMIHIC&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 16:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482538#M286870</guid>
      <dc:creator>Amihic</dc:creator>
      <dc:date>2018-07-30T16:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select the kth nonmissing variable or the first nonzero variable in an array?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482542#M286871</link>
      <description>&lt;P&gt;Use an ARRAY and a DO loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input 
ID  $ Y1994  Y1996  Y1998  Y2000 Y2002 Y2004 ;
cards;
01      .            .            .           .          12        12
02      .            .            .           .            .           . 
03      .            14         .            .           .           .
04      0           15         15         .           .         15
05       .            0          17         16         .          .
;

data want ;
 set have ;
 array y y:;
 do i=1 to dim(y) until (a not in (.,0)) ;
   a=y(i);
 end;
run;
proc print;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 373px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/22112i6E981B9C62BA4C96/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Jul 2018 16:53:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-select-the-kth-nonmissing-variable-or-the-first-nonzero/m-p/482542#M286871</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-07-30T16:53:10Z</dc:date>
    </item>
  </channel>
</rss>

