<?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: first and second non-missing value in a row in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367953#M87666</link>
    <description>&lt;P&gt;I think you marked the wrong answer as correct. &amp;nbsp;Use the answer with the WHILE () condition instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Update the test data to have a case with more than two distinct values to see the difference.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input a b c d e expect1 expect2 ;
cards;
8 8 8 4 8 8 4
5 8 6 . . 5 8
. . . 4 8 4 8
1 1 1 1 1 1 .
. . . . . . .
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can also change your upper and lower bounds on the DO loop. No need to check the first item since it is either missing or equal to the previously calculated FIRST value. &amp;nbsp;Use the DIM() function to make it easier to change the number of elements in the array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array value a b c d e;
  first= coalesce(of value(*));
  do i = 2 to dim(value) while (missing(second));
    if value(i) ^= first then second = value(i);
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could calculate FIRST and SECOND in the same loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array value a b c d e;
  do i = 1 to dim(value) while (missing(second));
    if missing(first) then first=value(i);
    else if value(i) ^= first then second = value(i);
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 17 Jun 2017 16:00:35 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2017-06-17T16:00:35Z</dc:date>
    <item>
      <title>first and second non-missing value in a row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367745#M87600</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Hello &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I have a problem with my code.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I want to find the first and second non-missing value in a row so that the second one is not equal to the first one. &lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;array value a-e;&lt;/P&gt;&lt;P&gt;array nvalue(&lt;STRONG&gt;5&lt;/STRONG&gt;) _temporary_;&lt;/P&gt;&lt;P&gt;first= coalesce(of value(*));&lt;/P&gt;&lt;P&gt;do i = &lt;STRONG&gt;1&lt;/STRONG&gt; to &lt;STRONG&gt;5&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;if value(i) ^= first then second = value (i);&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;drop i;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;my result is like this:&lt;/P&gt;&lt;P&gt;A b c d e &amp;nbsp; &amp;nbsp;&amp;nbsp; first&amp;nbsp;&amp;nbsp;&amp;nbsp; second&lt;/P&gt;&lt;P&gt;8 8 8 4 8 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;/P&gt;&lt;P&gt;5 8 .&amp;nbsp; .&amp;nbsp; .&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; .&lt;/P&gt;&lt;P&gt;.&amp;nbsp; .&amp;nbsp; . 4&amp;nbsp; 8&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just those rows like the second row has problem. It shows ( . ) for the second one.&lt;/P&gt;&lt;P&gt;Thanks a lot&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 14:52:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367745#M87600</guid>
      <dc:creator>ali_far</dc:creator>
      <dc:date>2017-06-16T14:52:10Z</dc:date>
    </item>
    <item>
      <title>Re: first and second non-missing value in a row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367747#M87601</link>
      <description>&lt;P&gt;Try&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if value{i} ne first and not missing(value{i}) then second = value {i};&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 16 Jun 2017 14:56:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367747#M87601</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-06-16T14:56:29Z</dc:date>
    </item>
    <item>
      <title>Re: first and second non-missing value in a row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367816#M87627</link>
      <description>&lt;P&gt;I'd recommend changing the DO loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;do i = 1 to 5 until (second &amp;gt; . ) ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your loop (in its current state) continues even after populating SECOND. &amp;nbsp;So it can replace a valid SECOND with a missing value.&lt;/P&gt;</description>
      <pubDate>Fri, 16 Jun 2017 17:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367816#M87627</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-06-16T17:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: first and second non-missing value in a row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367925#M87656</link>
      <description>&lt;P&gt;thanks alot&lt;/P&gt;</description>
      <pubDate>Sat, 17 Jun 2017 02:10:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367925#M87656</guid>
      <dc:creator>ali_far</dc:creator>
      <dc:date>2017-06-17T02:10:53Z</dc:date>
    </item>
    <item>
      <title>Re: first and second non-missing value in a row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367926#M87657</link>
      <description>Thanks a lot</description>
      <pubDate>Sat, 17 Jun 2017 02:12:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367926#M87657</guid>
      <dc:creator>ali_far</dc:creator>
      <dc:date>2017-06-17T02:12:47Z</dc:date>
    </item>
    <item>
      <title>Re: first and second non-missing value in a row</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367953#M87666</link>
      <description>&lt;P&gt;I think you marked the wrong answer as correct. &amp;nbsp;Use the answer with the WHILE () condition instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Update the test data to have a case with more than two distinct values to see the difference.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  input a b c d e expect1 expect2 ;
cards;
8 8 8 4 8 8 4
5 8 6 . . 5 8
. . . 4 8 4 8
1 1 1 1 1 1 .
. . . . . . .
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can also change your upper and lower bounds on the DO loop. No need to check the first item since it is either missing or equal to the previously calculated FIRST value. &amp;nbsp;Use the DIM() function to make it easier to change the number of elements in the array.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array value a b c d e;
  first= coalesce(of value(*));
  do i = 2 to dim(value) while (missing(second));
    if value(i) ^= first then second = value(i);
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you could calculate FIRST and SECOND in the same loop.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array value a b c d e;
  do i = 1 to dim(value) while (missing(second));
    if missing(first) then first=value(i);
    else if value(i) ^= first then second = value(i);
  end;
  drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Jun 2017 16:00:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-and-second-non-missing-value-in-a-row/m-p/367953#M87666</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-06-17T16:00:35Z</dc:date>
    </item>
  </channel>
</rss>

