<?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 How to Swap first and last record using Temporary Arrays in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777592#M247408</link>
    <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;&lt;P&gt;I am Trying to Swap the values of first and last record using Temporary Arrays.&lt;/P&gt;&lt;P&gt;data list;&lt;BR /&gt;infile datalines dsd;&lt;BR /&gt;input Num @@;&lt;BR /&gt;datalines;&lt;BR /&gt;12, 35, 9, 56, 24&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%Macro swap();&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select Num into :Num1-&lt;BR /&gt;from list;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Data result;&lt;BR /&gt;set list;&lt;BR /&gt;Array t[1:&amp;amp;sqlobs.] _TEMPORARY_;&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;sqlobs.;&lt;BR /&gt;t[&amp;amp;i.]=&amp;amp;&amp;amp;Num&amp;amp;i.;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%if 1 %then %do;&lt;BR /&gt;_i=t[1];&lt;BR /&gt;t[1]=t[5];&lt;BR /&gt;t[5]=_i;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend swap;&lt;/P&gt;&lt;P&gt;%swap();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please tell me how to store the Temporary Array values into a permanent Variable and get the final output as :- 24, 35, 9, 56, 12&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 31 Oct 2021 16:05:31 GMT</pubDate>
    <dc:creator>anshul_9</dc:creator>
    <dc:date>2021-10-31T16:05:31Z</dc:date>
    <item>
      <title>How to Swap first and last record using Temporary Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777592#M247408</link>
      <description>&lt;P&gt;Hello Everyone,&lt;/P&gt;&lt;P&gt;I am Trying to Swap the values of first and last record using Temporary Arrays.&lt;/P&gt;&lt;P&gt;data list;&lt;BR /&gt;infile datalines dsd;&lt;BR /&gt;input Num @@;&lt;BR /&gt;datalines;&lt;BR /&gt;12, 35, 9, 56, 24&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%Macro swap();&lt;BR /&gt;proc sql noprint;&lt;BR /&gt;select Num into :Num1-&lt;BR /&gt;from list;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Data result;&lt;BR /&gt;set list;&lt;BR /&gt;Array t[1:&amp;amp;sqlobs.] _TEMPORARY_;&lt;/P&gt;&lt;P&gt;%do i=1 %to &amp;amp;sqlobs.;&lt;BR /&gt;t[&amp;amp;i.]=&amp;amp;&amp;amp;Num&amp;amp;i.;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%if 1 %then %do;&lt;BR /&gt;_i=t[1];&lt;BR /&gt;t[1]=t[5];&lt;BR /&gt;t[5]=_i;&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;%mend swap;&lt;/P&gt;&lt;P&gt;%swap();&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can anyone please tell me how to store the Temporary Array values into a permanent Variable and get the final output as :- 24, 35, 9, 56, 12&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 16:05:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777592#M247408</guid>
      <dc:creator>anshul_9</dc:creator>
      <dc:date>2021-10-31T16:05:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to Swap first and last record using Temporary Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777593#M247409</link>
      <description>&lt;P&gt;Macros and temporary arrays? I don't think those are needed at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
infile datalines dsd;
input Num @@;
datalines;
12, 35, 9, 56, 24
;
run;

proc transpose data=list out=list_t prefix=t;
    var num;
run;

data want;
    set list_t;
    _i=t1;
    t1=t5;
    t5=_i;
    drop _i;
run;
	&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 16:21:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777593#M247409</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-10-31T16:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to Swap first and last record using Temporary Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777597#M247411</link>
      <description>&lt;P&gt;If you have more than 2 obs. in the dataset, this one works too:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data list;
infile datalines dsd;
input Num @@;
datalines;
12, 35, 9, 56, 24
;
run;
proc print;
run;

data want;
  do point=nobs,2 to nobs-1,1;
    set list point=point nobs=nobs;
    output;
  end;
STOP;
run;
proc print;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 18:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777597#M247411</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2021-10-31T18:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to Swap first and last record using Temporary Arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777616#M247424</link>
      <description>&lt;P&gt;Array as a construct exist per observation. So typically any array based approach to working across record/observation boundaries is likely to be suspect. Consider as a minimum do you know before you start what the maximum number of records that you need to process is? You have to define the array to handle that.&lt;/P&gt;
&lt;P&gt;The sql approach you are attempting would be a nightmare if you have one or more Id variables to designate groups that need similar processing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example this accomplishes what I think you expect for output (your description lacks some precision as to actual desired output)&lt;/P&gt;
&lt;PRE&gt;data junk;
   set list (firstobs=5)
       list (firstobs=2 obs=4)
       list (firstobs=1 obs=1)
   ;
run;&lt;/PRE&gt;
&lt;P&gt;But this would be pretty obnoxious to modify for any sort of grouping variables.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Oct 2021 23:28:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Swap-first-and-last-record-using-Temporary-Arrays/m-p/777616#M247424</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-10-31T23:28:48Z</dc:date>
    </item>
  </channel>
</rss>

