<?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 to flip the order of an array in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825211#M325944</link>
    <description>&lt;P&gt;For numbered range lists (as in your second example) you can reverse the order without an array:&lt;/P&gt;
&lt;PRE&gt;data have;
array tf[4] $ ('csv' 'xlsx' ' ' ' ');
run;

data want;
set have;
lastnonmiss=whichc(coalescec(of &lt;FONT size="4" color="#3366FF"&gt;&lt;STRONG&gt;tf4-tf1&lt;/STRONG&gt;&lt;/FONT&gt;), of tf1-tf4);
run;&lt;/PRE&gt;
&lt;P&gt;These reversed ranges can also be used in an array definition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sashelp.pricedata;
array revprice[*] price17-price1;
/* ... */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(so that &lt;FONT face="courier new,courier"&gt;revprice[1]&lt;/FONT&gt; equals &lt;FONT face="courier new,courier"&gt;price17&lt;/FONT&gt;,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;revprice[2]&lt;/FONT&gt; equals &lt;FONT face="courier new,courier"&gt;price16&lt;/FONT&gt;, etc.).&lt;/P&gt;</description>
    <pubDate>Mon, 25 Jul 2022 11:41:26 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2022-07-25T11:41:26Z</dc:date>
    <item>
      <title>How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825195#M325935</link>
      <description>&lt;P&gt;In a code snippet not shown here I use the coalesce(c) functions in an array context.&lt;/P&gt;
&lt;P&gt;But I want the coalesce function to start from the last element of the array.&lt;/P&gt;
&lt;P&gt;I've found a workaround for my code but I wonder if I can invert the order of an array.&lt;/P&gt;
&lt;P&gt;For sure I could use 'do dim(array) to 1...' but this doesn't work for 'of array[*]' usage.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in my basic example I want to array _var to have weight as the first element without changing the order for which it was defined.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data class;
set sashelp.class;
run;

data class2;
set class;
array _var (3) Age Height Weight;
array _retain (3) _Age _Height _Weight;
array _mult (3)  (1 2 3);
do i=1 to dim (_var);
_retain(i)=_var(i)*_mult(i);
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;array temp1 (4) type_files1 type_files2 type_files3 type_files4;

lastnonmiss1=whichc(coalescec(of temp1[*]), of temp1[*]);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 09:14:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825195#M325935</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-07-25T09:14:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825202#M325937</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;loop from last to first and compare manually:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=dim(temp1) to 1;
   if vvalue(temp1[i]) eq coalescec(of temp1[*]) then lastnonmiss1=i;
end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jul 2022 09:54:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825202#M325937</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-07-25T09:54:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825204#M325938</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/77163"&gt;@Oligolas&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;loop from last to first and compare manually:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=dim(temp1) to 1;
   if vvalue(temp1[i]) eq coalescec(of temp1[*]) then lastnonmiss1=i;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;UNTESTED but shouldn't that say&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;do i=dim(temp1) to 1 by -1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 09:57:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825204#M325938</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-25T09:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825206#M325939</link>
      <description>&lt;P&gt;Thanks, nice trick but it doesn't solve my challenge.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The array elements were constructed from left to right if available meaning that if my array dimension is 4 then the elements could be&lt;/P&gt;
&lt;P&gt;temp1(1)=csv&lt;/P&gt;
&lt;P&gt;temp1(2)=xlsx&lt;/P&gt;
&lt;P&gt;temp1(3)=''&lt;/P&gt;
&lt;P&gt;temp1(4)=''&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your code still resolves to 1 (or 4 in your inverse order) as the condition becomes true for the coalescec result that in my understanding is still the first array element.&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 10:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825206#M325939</guid>
      <dc:creator>acordes</dc:creator>
      <dc:date>2022-07-25T10:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825209#M325942</link>
      <description>&lt;P&gt;Reverse an array like this, then you can do anything you want with it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
    array temp temp1-temp4;
    array reverse(4) _temporary_;
    temp1=1;
    temp2=2;
    temp3=.;
    temp4=.;
    do i=1 to dim(temp);
        reverse(i)=temp(dim(temp)-i+1);
    end;
    calc = coalesce(of reverse{*});
    drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jul 2022 11:12:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825209#M325942</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-25T11:12:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825210#M325943</link>
      <description>&lt;P&gt;With FCMP you can write your own COALESCECR function, however due to limitations of FCMP the implementation would accept only a character array (not an OF var(*) argument which expands to a variable number of individual arguments), OR a fixed number of arguments (not useful)&lt;/P&gt;
&lt;LI-SPOILER&gt;&lt;LI-CODE lang="sas"&gt;options cmplib='';

proc fcmp outlib=sasuser.sandbox.udf;

  function coalescecR(s(*) $) $ 200;
    do index = dim(s) to 1 by -1;
      if not missing(s[index]) then return (s[index]);
    end; 
    
    return ('');
  endsub;

quit;

options cmplib=sasuser.sandbox;&lt;/LI-CODE&gt;&lt;/LI-SPOILER&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array s a b c d e f g;
  lastc = coalescecR(s);                /* &amp;lt;------------------- */
  put lastc=;
run;
&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jul 2022 11:25:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825210#M325943</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2022-07-25T11:25:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825211#M325944</link>
      <description>&lt;P&gt;For numbered range lists (as in your second example) you can reverse the order without an array:&lt;/P&gt;
&lt;PRE&gt;data have;
array tf[4] $ ('csv' 'xlsx' ' ' ' ');
run;

data want;
set have;
lastnonmiss=whichc(coalescec(of &lt;FONT size="4" color="#3366FF"&gt;&lt;STRONG&gt;tf4-tf1&lt;/STRONG&gt;&lt;/FONT&gt;), of tf1-tf4);
run;&lt;/PRE&gt;
&lt;P&gt;These reversed ranges can also be used in an array definition:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sashelp.pricedata;
array revprice[*] price17-price1;
/* ... */
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;(so that &lt;FONT face="courier new,courier"&gt;revprice[1]&lt;/FONT&gt; equals &lt;FONT face="courier new,courier"&gt;price17&lt;/FONT&gt;,&amp;nbsp;&lt;FONT face="courier new,courier"&gt;revprice[2]&lt;/FONT&gt; equals &lt;FONT face="courier new,courier"&gt;price16&lt;/FONT&gt;, etc.).&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 11:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825211#M325944</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-25T11:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825224#M325949</link>
      <description>&lt;PRE&gt;data class;
set sashelp.class;
run;

proc sql noprint;
select name into :vnames separated by ' '
 from dictionary.columns 
  where libname='WORK' and upcase(name) in ('AGE' 'WEIGHT' 'HEIGHT')
   order by varnum desc;
quit;

%put &amp;amp;=vnames. ;&lt;/PRE&gt;</description>
      <pubDate>Mon, 25 Jul 2022 12:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825224#M325949</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-07-25T12:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825225#M325950</link>
      <description>You could make a macro to contain the variable name which have the reverse order. As above.&lt;BR /&gt;And refer to it in your code:&lt;BR /&gt;&lt;BR /&gt;data class2;&lt;BR /&gt;set class;&lt;BR /&gt;array _var{*}  &amp;amp;vnames. ;&lt;BR /&gt;......................</description>
      <pubDate>Mon, 25 Jul 2022 12:39:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825225#M325950</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-07-25T12:39:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825239#M325957</link>
      <description>&lt;P&gt;of course, it's by -1&lt;/P&gt;
&lt;P&gt;My mistake&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 14:24:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825239#M325957</guid>
      <dc:creator>Oligolas</dc:creator>
      <dc:date>2022-07-25T14:24:50Z</dc:date>
    </item>
    <item>
      <title>Re: How to flip the order of an array</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825246#M325960</link>
      <description>&lt;P&gt;Why not just list the elements in the opposite order?&lt;/P&gt;
&lt;P&gt;The code you posted that is using the array to perform calculations does not depend on the order that the variables are listed in the array definition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you have a simple list of variables with a numeric suffix then you can make a variable list from last to first as easily as from first to last.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also there is no need to define an array to use the coalescec() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;lastnonmiss1=coalescec(of type_files4-type_files1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Jul 2022 14:43:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-flip-the-order-of-an-array/m-p/825246#M325960</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-25T14:43:13Z</dc:date>
    </item>
  </channel>
</rss>

