<?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: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746849#M234341</link>
    <description>This may get you there but you may also need more to account for ties. &lt;BR /&gt;Test it and let me know if meets your needs.&lt;BR /&gt;&lt;BR /&gt;if not missing(index) then  names[i] = vname(values[index]);</description>
    <pubDate>Wed, 09 Jun 2021 18:25:40 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2021-06-09T18:25:40Z</dc:date>
    <item>
      <title>Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746821#M234319</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Below are the data I have and data I want:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data readin;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;input y1-y6;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;cards;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;11 55 59 35 25 87&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;12 79 73 74 86 29&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;13 80 95 77 25 74&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data want;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;set readin;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;array values[*] y1-y6;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;array large[3];&lt;BR /&gt;&amp;nbsp; &amp;nbsp;array names[3] $32;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;do i = 1 to dim(large);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; large[i] = largest(i,of values[*]);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; index &amp;nbsp; &amp;nbsp;= whichn(large[i],of values[*]);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; names[i] = vname(values[index]);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;drop i index;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&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;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="twerwath_0-1623259760933.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/60197i94B8CCF83DB1C66E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="twerwath_0-1623259760933.png" alt="twerwath_0-1623259760933.png" /&gt;&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;This code works as intended, except for the line with the VNAME statement (line 9). SAS returns the error "array subscript out of range" for this line. It runs successfully if that line is removed. To me, the logic makes sense, so I am not understanding why I am receiving this error. Can anyone enlighten me or provide a working code for this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 17:33:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746821#M234319</guid>
      <dc:creator>twerwath</dc:creator>
      <dc:date>2021-06-09T17:33:36Z</dc:date>
    </item>
    <item>
      <title>Re: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746826#M234321</link>
      <description>Please show the log and error. It looks like you haven't handled the case when the index isn't valid - which may happen if you have missing values or ties. &lt;BR /&gt;Also, this would be drastically simpler in a long data set rather than a wide data set. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Jun 2021 17:41:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746826#M234321</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-09T17:41:06Z</dc:date>
    </item>
    <item>
      <title>Re: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746831#M234326</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Yes, this appears to be the case, thank you. Here is a slightly edited readin dataset which produces the error:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data readin;&lt;BR /&gt;input y1-y6;&lt;BR /&gt;cards;&lt;BR /&gt;11 55 11 35 . 87&lt;BR /&gt;12 12 12 . . .&lt;BR /&gt;13 80 95 77 25 74&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;810&lt;BR /&gt;811&lt;BR /&gt;812 data want;&lt;BR /&gt;813 set readin;&lt;BR /&gt;814 array values[*] y1-y6;&lt;BR /&gt;815 array large[5];&lt;BR /&gt;816 array names[5] $32;&lt;BR /&gt;817 do i = 1 to dim(large);&lt;BR /&gt;818 large[i] = largest(i,of values[*]);&lt;BR /&gt;819 index = whichn(large[i],of values[*]);&lt;BR /&gt;820 names[i] = vname(values[index]);&lt;BR /&gt;821 end;&lt;BR /&gt;822 drop i index;&lt;BR /&gt;823&lt;BR /&gt;824 run;&lt;/P&gt;
&lt;P&gt;ERROR: Array subscript out of range at line 820 column 24.&lt;BR /&gt;y1=12 y2=12 y3=12 y4=. y5=. y6=. large1=12 large2=12 large3=12 large4=. large5=. names1=y1 names2=y1&lt;BR /&gt;names3=y1 names4= names5= i=4 index=. _ERROR_=1 _N_=2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is there any possible solution to this?&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 17:53:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746831#M234326</guid>
      <dc:creator>twerwath</dc:creator>
      <dc:date>2021-06-09T17:53:25Z</dc:date>
    </item>
    <item>
      <title>Re: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746834#M234329</link>
      <description>Well, what do you want to do when you don't have a top 3? And only have two or one?&lt;BR /&gt;</description>
      <pubDate>Wed, 09 Jun 2021 17:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746834#M234329</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-09T17:55:37Z</dc:date>
    </item>
    <item>
      <title>Re: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746844#M234337</link>
      <description>&lt;P&gt;It would be best if missings were ignored or moved to the bottom, such that there would only one value listed in variable large1 if only one is non missing, only values listed in large1 and large2 if only two are non missing, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As for ties, i'm not sure. There isn't really any preference for order in cases where the values are the same.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 18:16:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746844#M234337</guid>
      <dc:creator>twerwath</dc:creator>
      <dc:date>2021-06-09T18:16:01Z</dc:date>
    </item>
    <item>
      <title>Re: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746848#M234340</link>
      <description>&lt;P&gt;You have missing values (or ties?) such that there are not 3 "largest" values in some observations.&lt;/P&gt;
&lt;P&gt;Just test before using the value of INDEX.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;      if index then names[i] = vname(values[index]);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data readin;
input y1-y6;
cards;
11 55 59 35 25 87
12 79 73 74 86 29
13 80 95 77 25 74
. . . . . .
;
run;

 

data want;
   set readin;
   array values[*] y1-y6;
   array large[3];
   array names[3] $32;
   do i = 1 to dim(large);
      large[i] = largest(i,of values[*]);
      index    = whichn(large[i],of values[*]);
      if index then names[i] = vname(values[index]);
   end;
   drop i index;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 09 Jun 2021 18:25:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746848#M234340</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-09T18:25:39Z</dc:date>
    </item>
    <item>
      <title>Re: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746849#M234341</link>
      <description>This may get you there but you may also need more to account for ties. &lt;BR /&gt;Test it and let me know if meets your needs.&lt;BR /&gt;&lt;BR /&gt;if not missing(index) then  names[i] = vname(values[index]);</description>
      <pubDate>Wed, 09 Jun 2021 18:25:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746849#M234341</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-09T18:25:40Z</dc:date>
    </item>
    <item>
      <title>Re: Receiving error when using VNAME in DO STEP to identify top 3 values in ARRAY</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746851#M234343</link>
      <description>&lt;P&gt;Of course, that makes sense. Not sure why I didn't think of this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It seems both answers are basically the same. I tested it and this works well enough for my purposes. Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Jun 2021 18:32:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Receiving-error-when-using-VNAME-in-DO-STEP-to-identify-top-3/m-p/746851#M234343</guid>
      <dc:creator>twerwath</dc:creator>
      <dc:date>2021-06-09T18:32:50Z</dc:date>
    </item>
  </channel>
</rss>

