<?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: ARRAYS- How to pick last 2 non-missing values and pick corresponding vars based on their positio in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602829#M174575</link>
    <description>Thank you!</description>
    <pubDate>Fri, 08 Nov 2019 19:29:56 GMT</pubDate>
    <dc:creator>ahhh</dc:creator>
    <dc:date>2019-11-08T19:29:56Z</dc:date>
    <item>
      <title>ARRAYS- How to pick last 2 non-missing values and pick corresponding vars based on their positions</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602682#M174512</link>
      <description>&lt;P&gt;Hi, Need help solving this-&lt;/P&gt;&lt;P&gt;My dataset looks like this:&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input id value1 value2 value3 value4 value5 dt1 :date9. dt2 :date9. dt3 :date9. dt4 :date9. dt5 :date9.;&lt;BR /&gt;format dt1 dt2 dt3 dt4 dt5 date9.;&lt;BR /&gt;datalines;&lt;/P&gt;&lt;P&gt;1 300 400 60 . 40 01feb2015 03mar2016 21apr2016 . 21may2016&lt;BR /&gt;2 1200 300 40 40 . 05jan2012 04feb2012 15apr2013 23apr2014 .&lt;BR /&gt;3 30 60 100 . 200 09dec2011 10jan2011 15feb2011 . 16apr2013&lt;BR /&gt;4 . . 40 30 200 . . 12feb2011 16mar2011 17apr2012&lt;BR /&gt;5 200 200 200 20 20 10sep2010 18oct2010 13dec2010 23apr2013 26oct2014&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dt1 corresponds to value1, date2 corresponds to value2 etc..&lt;/P&gt;&lt;P&gt;I need to pull last two non-missing 'value' variables if they are under a certain value -say 100 and then their corresponding dates and calculate difference period in days between them&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what I want:&lt;/P&gt;&lt;P&gt;For ids 3 and 4: they don't qualify as atleast one of the last two non-missing values is greater than 100.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id lastnonmiss last2ndnonmiss dt_lastnonmiss dt_last2ndnonmiss timediffindays&lt;/P&gt;&lt;P&gt;1 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;60&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 21may2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;21apr2016&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 30&lt;/P&gt;&lt;P&gt;2 40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;40&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;23apr2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 15apr2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;373&lt;/P&gt;&lt;P&gt;5 20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;26oct2014&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;23apr2013&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;551&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried using Coalesce function (which helped me pick the last non-missing and last 2nd nonmissing values) but unable to pick corresponding dates. Also, needed to include the initial condition that the last and 2nd last non-missing values should be less than 100. Please help ASAP! Much appreciated!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 07:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602682#M174512</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2019-11-08T07:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAYS- How to pick last 2 non-missing values and pick corresponding vars based on their positio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602689#M174518</link>
      <description>&lt;P&gt;Can you show us the code you have tried so far?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a brute force approach&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   format lastnonmiss last2ndnonmiss dt_lastnonmiss dt_last2ndnonmiss timediffindays;

   set have;
   array v {5} value5-value1;
   array d {5} dt5-dt1;

   lastnonmiss=coalesce(of v[*]);
   dt_lastnonmiss=d[whichn(lastnonmiss, of v[*])];

   v[whichn(lastnonmiss, of v[*])]=.;

   last2ndnonmiss=coalesce(of v[*]);
   dt_last2ndnonmiss=d[whichn(last2ndnonmiss, of v[*])];

   if lastnonmiss &amp;lt;= 100 &amp;amp; last2ndnonmiss &amp;lt;= 100;

   timediffindays=dt_lastnonmiss-dt_last2ndnonmiss;

   format dt: date9.;
   keep lastnonmiss last2ndnonmiss dt_lastnonmiss dt_last2ndnonmiss timediffindays;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;lastnonmiss last2ndnonmiss dt_lastnonmiss dt_last2ndnonmiss timediffindays 
40          60             21MAY2016      21APR2016         30 
40          40             23APR2014      15APR2013         373 
20          20             26OCT2014      23APR2013         551 
&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Nov 2019 08:59:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602689#M174518</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-11-08T08:59:37Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAYS- How to pick last 2 non-missing values and pick corresponding vars based on their positio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602696#M174522</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/119710"&gt;@ahhh&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Keep your VALUES in one array.&lt;/P&gt;
&lt;P&gt;Keep your DATES in another array.&lt;/P&gt;
&lt;P&gt;Have an array sized to 2 for holding the selected VALUES.&lt;/P&gt;
&lt;P&gt;Similarly use an array sized to 2 to hold the corresponding DATES.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id value1 value2 value3 value4 value5 dt1 :date9. dt2 :date9. dt3 :date9. 
                           dt4 :date9. dt5 :date9.;
format dt1 dt2 dt3 dt4 dt5 date9.;
datalines;

1 300 400 60 . 40 01feb2015 03mar2016 21apr2016 . 21may2016
2 1200 300 40 40 . 05jan2012 04feb2012 15apr2013 23apr2014 .
3 30 60 100 . 200 09dec2011 10jan2011 15feb2011 . 16apr2013
4 . . 40 30 200 . . 12feb2011 16mar2011 17apr2012
5 200 200 200 20 20 10sep2010 18oct2010 13dec2010 23apr2013 26oct2014
;
run;

data want;
   set have;
   array v value1 - value5;
   array d dt1 - dt5;
   array tv[2] _temporary_;
   array td[2] _temporary_;
   count = 0;
   do i = dim(v) to 1 by -1;
      if v[i] = . then continue;
      if v[i] Ge 100 then leave;
      count + 1;
      tv[count] = v[i];
      td[count] = d[i];
         
      if count = 2 then do;
         datediff = td[1] - td[2];
         output;
         leave;
      end;
   end;
drop count i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture_01.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/33776i7BB806D8241CB4A4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture_01.JPG" alt="Capture_01.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2019 09:56:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602696#M174522</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-11-08T09:56:18Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAYS- How to pick last 2 non-missing values and pick corresponding vars based on their positio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602829#M174575</link>
      <description>Thank you!</description>
      <pubDate>Fri, 08 Nov 2019 19:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602829#M174575</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2019-11-08T19:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: ARRAYS- How to pick last 2 non-missing values and pick corresponding vars based on their positio</title>
      <link>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602830#M174576</link>
      <description>Thank you!</description>
      <pubDate>Fri, 08 Nov 2019 19:30:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/ARRAYS-How-to-pick-last-2-non-missing-values-and-pick/m-p/602830#M174576</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2019-11-08T19:30:43Z</dc:date>
    </item>
  </channel>
</rss>

