<?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: Finding 2 minimum value using array and return column names in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577135#M13149</link>
    <description>&lt;PRE&gt; cur_offer offer1 - offer10;
   5            1 2 3 4 5 6  7  6  6  10&lt;/PRE&gt;
&lt;P&gt;In this case what are the expected values for Best_offer1 and Best_offer2? The first smallest greater than 5 is 6 and so Best_offer1 = 6. The question what would you like to have Best_offer2? Is it 8 or 7?&lt;/P&gt;</description>
    <pubDate>Sat, 27 Jul 2019 07:57:59 GMT</pubDate>
    <dc:creator>KachiM</dc:creator>
    <dc:date>2019-07-27T07:57:59Z</dc:date>
    <item>
      <title>Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577127#M13147</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; I need you guys' support to help me figure it out this problem. What I am working on is that &lt;EM&gt;I would like to find 2 minimum value in the column called "Offer1 - Offer10" and return the column name with the condition that these 2 minimum values need to be greater than the reference value (called "Current Offer") and in case there is no any value in "Offer1 - Offer10" then return as "No Offer"&lt;/EM&gt;. What I could do now is I could find the minimum value and return the column names but &lt;EM&gt;am currently struggling when there is more condition.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Example:&lt;/STRONG&gt; No1. =&amp;gt; Current Offer is 3 then 2 minimum offer can be 6 and 7.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; No2. =&amp;gt; Current Offer is 4 then 2 minimum offer I would like is 5 and 6.&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; No6. =&amp;gt; Current Offer is 5 and no any value in Offer1-Offer10 then return as "No Offer"&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; No8. =&amp;gt; Current Offer is 4 and only one value (Offer10) then return as 10.&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="Offer.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31336i6A467E01CA0DB36B/image-size/large?v=v2&amp;amp;px=999" role="button" title="Offer.png" alt="Offer.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for all your contribution and support.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;/P&gt;&lt;P&gt;Jame.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jul 2019 05:11:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577127#M13147</guid>
      <dc:creator>jamerse</dc:creator>
      <dc:date>2019-07-27T05:11:44Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577135#M13149</link>
      <description>&lt;PRE&gt; cur_offer offer1 - offer10;
   5            1 2 3 4 5 6  7  6  6  10&lt;/PRE&gt;
&lt;P&gt;In this case what are the expected values for Best_offer1 and Best_offer2? The first smallest greater than 5 is 6 and so Best_offer1 = 6. The question what would you like to have Best_offer2? Is it 8 or 7?&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jul 2019 07:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577135#M13149</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-27T07:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577136#M13150</link>
      <description>&lt;P&gt;Thank you datasp for looking it up. My expected result for Best_Offer1 is 6 and Best_Offer2 is 7.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jul 2019 08:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577136#M13150</guid>
      <dc:creator>jamerse</dc:creator>
      <dc:date>2019-07-27T08:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577140#M13151</link>
      <description>&lt;P&gt;The following steps are followed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[1] Copy offer1 - offer10 ( array o[ ]) into another _temporary_ array (ORIG[ ]) for lookups.&amp;nbsp; _temporary_ array will be filled with missing values at the beginning of the data step iteration. Copy to it all values greater than cut_offer at the position as in array offer1 - offer10.&lt;/P&gt;
&lt;P&gt;[2] Sort the ORIG[ ] array. This will place all the missing values to the beginning of the array shifting non-missings to the right.&lt;/P&gt;
&lt;P&gt;[3] Using WHICHN() function we search for the position of the first and second non-missing values in array o[ ](for Best_offer1 and Best_offer2).&lt;/P&gt;
&lt;P&gt;[4] When Best_offer1 = Best_offer2, then search for the next non-missing value and its corresponding position in array o[ ]&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have added some rows for testing purpose.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
input cur_offer offer1 - offer10;
datalines;
3 . . . . . 6  7  8  9  10
4 1 2 . . 5 6  7  8  9  10
4 1 2 . . 9 6  7  8  5  10
2 . . 3 4 5 6  7  8  9  10
4 . . . 4 5 6  7  8  9  10
1 1 2 3 4 5 6  7  8  9  10
5 . . . . . .  .  .  .   .
5 1 2 . . . .  7  8  9  10
4 . . . . . .  .  .  .  10
4 . . . . . .  .  .  .   .                        
9 . . . 3 . 6  .  .  .  10
5 1 2 3 4 5 6  7  6  6  10
;
run;&lt;/PRE&gt;
&lt;P&gt;The program follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   array o offer1 - offer10;
   array orig[10] _temporary_;
   call missing(of orig[*]);
   do i = 1 to dim(o);
      if o[i] &amp;gt; cur_offer then orig[i] = o[i];
   end;
   call sortn(of orig[*]);

   Best_offer1 = .; Best_offer2 = .;
   do i = 1 to dim(o);
      if missing(orig[i]) then continue;
      Best_offer1 = whichN(orig[i], of o[*]);
      j = i+1;
      if j &amp;lt;= dim(o) then Best_offer2 = whichN(orig[j], of o[*]);
      do while( j &amp;lt;= dim(o) and Best_offer1 = Best_offer2);
         Best_offer2 = whichN(orig[j], of o[*]);
         j+1;
      end;
      leave;
   end;

drop i j;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there are any queries or difficulties please seek help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jul 2019 09:33:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577140#M13151</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-27T09:33:41Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577147#M13152</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cur_offer offer1 - offer10;
array x{*} offer1-offer10;
do i=1 to dim(x);
 if x{i}&amp;lt;=cur_offer then x{i}=.;
end;
smallest1=smallest(1,of x{*});
if not missing(smallest1) then smallest1_vname=vname(x{whichn(smallest1,of x{*})});
smallest2=smallest(2,of x{*});
if not missing(smallest2) then smallest2_vname=vname(x{whichn(smallest2,of x{*})});
datalines;
3 . . . . . 6  7  8  9  10
4 1 2 . . 5 6  7  8  9  10
4 1 2 . . 9 6  7  8  5  10
2 . . 3 4 5 6  7  8  9  10
4 . . . 4 5 6  7  8  9  10
1 1 2 3 4 5 6  7  8  9  10
5 . . . . . .  .  .  .   .
5 1 2 . . . .  7  8  9  10
4 . . . . . .  .  .  .  10
4 . . . . . .  .  .  .   .                        
9 . . . 3 . 6  .  .  .  10
5 1 2 3 4 5 6  7  6  6  10
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Jul 2019 11:35:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577147#M13152</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-27T11:35:10Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577148#M13153</link>
      <description>&lt;P&gt;The labeling part can be done as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   set have;
   array o offer1 - offer10;
   array label[10] $8 _temporary_ ('offer1' 'offer2' 'offer3' 'offer4' 'offer5'
                                   'offer6' 'offer7' 'offer8' 'offer9' 'offer10');
   array orig[10] _temporary_;
   call missing(of orig[*]);
   do i = 1 to dim(o);
      if o[i] &amp;gt; cur_offer then orig[i] = o[i];
   end;
   call sortn(of orig[*]);

   _offer1 = .; _offer2 = .;
   Best_offer1='No_offer';  Best_offer2='No_offer';
   do i = 1 to dim(o);
      if missing(orig[i]) then continue;
      _offer1 = whichN(orig[i], of o[*]);
      j = i+1;
      if j &amp;lt;= dim(o) then _offer2 = whichN(orig[j], of o[*]);
      do while( j &amp;lt;= dim(o) and _offer1 = _offer2);
         _offer2 = whichN(orig[j], of o[*]);
         j+1;
      end;
      leave;
   end;
   if _offer1 then Best_offer1 = label(_offer1);
   if _offer2 then Best_offer2 = label(_offer2);
drop i j;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also, Ksharp responded using Smallest() function. Choose whatever you like.&lt;/P&gt;</description>
      <pubDate>Sat, 27 Jul 2019 12:07:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577148#M13153</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-27T12:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577155#M13154</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/157742"&gt;@jamerse&lt;/a&gt;&amp;nbsp; &amp;nbsp;Assuming I understand your requirement&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input cur_offer offer1 - offer10;
datalines;
3	.	.	.	.	.	6	7	8	9	10
4	1	2	.	.	5	6	7	8	9	10
2	.	.	3	4	5	6	7	8	9	10
4	.	.	.	4	5	6	7	8	9	10
1	1	2	3	4	5	6	7	8	9	10
5	.	.	.	.	.	.	.	.	.	.
5	1	2	.	.	.	.	7	8	9	10
4	.	.	.	.	.	.	.	.	.	10
4	.	.	.	.	.	.	.	.	.	.
9	.	.	.	3	.	6	.	.	.	10
;
run;

data want;
set have;
array t offer1-offer10;
do _i=cur_offer+1 to dim(t);
 if t(_i)&amp;gt;. then do; 
 _j=sum(_j,1);
 if _j=1 then Best_offer1=vname(t(_i));
 else if _j=2 then Best_offer2=vname(t(_i));
 end;
 if _j=2 then leave;
end;
if cmiss(of Best_offer:)=2 then do;
Best_offer1='No Offer';
Best_offer2='No Offer';
end;
drop _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Jul 2019 19:25:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577155#M13154</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-07-27T19:25:17Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577167#M13155</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your solutions are very good. However, you have assumed sorted Array. The OP has clarified for values can be unsorted like in third row.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my revised solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data want;
   set have;
   array o offer1 - offer10;

   array orig[10] _temporary_;
   call missing(of orig[*]);
   do i = 1 to dim(o);
      if o[i] &amp;gt; cur_offer then orig[i] = o[i];
   end;
   call sortn(of orig[*]);

   _offer1 = .; _offer2 = .;
   Best_offer1='No_offer';  Best_offer2='No_offer';
   do i = 1 to dim(o);
      if missing(orig[i]) then continue;
      _offer1 = whichN(orig[i], of o[*]);
      j = i+1;
      if j &amp;lt;= dim(o) then _offer2 = whichN(orig[j], of o[*]);
      do while( j &amp;lt;= dim(o) and _offer1 = _offer2);
         _offer2 = whichN(orig[j], of o[*]);
         j+1;
      end;
      leave;
   end;
   if _offer1 then Best_offer1 = vname(o[_offer1]);
   if _offer2 then Best_offer2 = vname(o[_offer2]);
drop i j _:;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Jul 2019 15:32:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577167#M13155</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-27T15:32:53Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577205#M13158</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17813"&gt;@KachiM&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp; Thank you very much for your guys support. They are all awesome !!!!. It is more complicated than I thought. Thank you all again :).&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 04:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577205#M13158</guid>
      <dc:creator>jamerse</dc:creator>
      <dc:date>2019-07-28T04:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577206#M13159</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/157742"&gt;@jamerse&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for accepting my solution. A revised version was placed as part of my observation of &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 04:22:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577206#M13159</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-28T04:22:54Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577222#M13165</link>
      <description>&lt;P&gt;datasp,&lt;/P&gt;
&lt;P&gt;No. My code does not '&lt;SPAN&gt;&amp;nbsp;assumed sorted Array ' . &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 12:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577222#M13165</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-28T12:23:20Z</dc:date>
    </item>
    <item>
      <title>Re: Finding 2 minimum value using array and return column names</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577233#M13172</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your solution doesn't require sorted array. My observation on your solution which I missed to say was that the OP's requirement was not met for the 12-th row. OP wanted 'offer6' and 'offer7'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;DataSP&lt;/P&gt;</description>
      <pubDate>Sun, 28 Jul 2019 14:17:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Finding-2-minimum-value-using-array-and-return-column-names/m-p/577233#M13172</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-07-28T14:17:59Z</dc:date>
    </item>
  </channel>
</rss>

