<?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: swap obs first and last each other in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909436#M358705</link>
    <description>&lt;P&gt;If you want to swap two specific rows, like the ith and the jth row, obs= and firstobs= options would be very&amp;nbsp;&lt;SPAN&gt;efficiently.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set testy(firstobs=9 obs=9) testy(firstobs=2 obs=8) testy(firstobs=1 obs=1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;If you&amp;nbsp;want to swap two variant rows, like the first and last row, or some rows which be determined by a variable, you may need direct access method.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set testy nobs=nobs end=eof;
  if _n_=1 then set testy point=nobs;
  if eof then set testy point=eof;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 22 Dec 2023 07:52:38 GMT</pubDate>
    <dc:creator>whymath</dc:creator>
    <dc:date>2023-12-22T07:52:38Z</dc:date>
    <item>
      <title>swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909434#M358703</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testy;
     input a; 
     datalines;
1
2
3
4
5
6
7
8
9
;
run;
/*how to swap first obs and last obs each other in SAS*/

required output
9
2
3
4
5
6
7
8
1&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Question: How to swap values each other as above required output&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 07:20:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909434#M358703</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2023-12-22T07:20:28Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909435#M358704</link>
      <description>&lt;P&gt;Interesting task.&lt;/P&gt;
&lt;P&gt;Maybe something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   call symputx('lastObs', _nobs);
   call symputx('beforeLast', _nobs -1);
   if 0 then set work.have nobs=_nobs;
run;

data want;
   set 
      work.have(firstobs=&amp;amp;lastObs.)
      work.have(firstobs=2 obs=&amp;amp;beforeLast.)
      work.have(firstobs=1 obs=1)
   ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Dec 2023 07:42:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909435#M358704</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2023-12-22T07:42:14Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909436#M358705</link>
      <description>&lt;P&gt;If you want to swap two specific rows, like the ith and the jth row, obs= and firstobs= options would be very&amp;nbsp;&lt;SPAN&gt;efficiently.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set testy(firstobs=9 obs=9) testy(firstobs=2 obs=8) testy(firstobs=1 obs=1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;If you&amp;nbsp;want to swap two variant rows, like the first and last row, or some rows which be determined by a variable, you may need direct access method.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set testy nobs=nobs end=eof;
  if _n_=1 then set testy point=nobs;
  if eof then set testy point=eof;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Dec 2023 07:52:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909436#M358705</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2023-12-22T07:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909439#M358706</link>
      <description>&lt;P&gt;Hi Whymath&lt;/P&gt;
&lt;P&gt;Thank you for your solution&amp;nbsp;&lt;/P&gt;
&lt;P&gt;supposer we want swap range of values how to achieve&lt;/P&gt;
&lt;P&gt;example required output&lt;/P&gt;
&lt;P&gt;9&lt;BR /&gt;8&lt;BR /&gt;7&lt;BR /&gt;4&lt;BR /&gt;5&lt;BR /&gt;6&lt;BR /&gt;3&lt;BR /&gt;2&lt;BR /&gt;1&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 08:24:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909439#M358706</guid>
      <dc:creator>pavank</dc:creator>
      <dc:date>2023-12-22T08:24:45Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909442#M358707</link>
      <description>&lt;P&gt;What is the&amp;nbsp;&lt;U&gt;exact rule&lt;/U&gt; for this?&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 08:35:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909442#M358707</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-12-22T08:35:09Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909446#M358709</link>
      <description>&lt;P&gt;You can try something like this for the last wanted result you show:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _N_=nobs to nobs-2 by -1, 4 to nobs-3, 3 to 1 by -1;
    set have point=_N_ nobs=nobs;
    output;
   end;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Your original result with the same style of coding:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _N_=nobs, 2 to nobs-1, 1;
    set have point=_N_ nobs=nobs;
    output;
   end;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Dec 2023 12:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909446#M358709</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-12-22T12:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909506#M358721</link>
      <description>&lt;P&gt;Let us broaden the question a bit. WHY exactly is the order of values of a single variable important? As in, what impact does it have on analysis or reporting?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An abstract example like this is quite often amenable to a solution but then when you try to expand its use, such as to groups of values within an analysis group, think geographic region or demographic group, it becomes quite complex. But may not actually be needed depending on your actual use need.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2023 15:56:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909506#M358721</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-12-22T15:56:35Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909534#M358726</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then set have nobs=n_last point=n_last;
  else set have (firstobs=2) end=end_of_have;
  if end_of_have then set have (obs=1);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;STRIKE&gt;&lt;CODE class=" language-sas"&gt;data want;
  if _n_=1 then set sashelp.class nobs=n_last point=n_last;
  else set sashelp.class (firstobs=2)  sashelp.class (obs=1);
run;&lt;/CODE&gt;&lt;/STRIKE&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;</description>
      <pubDate>Sat, 23 Dec 2023 03:06:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909534#M358726</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-23T03:06:07Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909537#M358727</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi Whymath&lt;/P&gt;
&lt;P&gt;Thank you for your solution&amp;nbsp;&lt;/P&gt;
&lt;P&gt;supposer we want swap range of values how to achieve&lt;/P&gt;
&lt;P&gt;example required output&lt;/P&gt;
&lt;P&gt;9&lt;BR /&gt;8&lt;BR /&gt;7&lt;BR /&gt;4&lt;BR /&gt;5&lt;BR /&gt;6&lt;BR /&gt;3&lt;BR /&gt;2&lt;BR /&gt;1&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can (edited response):&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do i=1 to 9; output; end;
run;

%let groupsize_beg=3;
%let groupsize_end=3;

data want;
  do ptr= n_last to n_last +1 -&amp;amp;groupsize_end by -1
        , &amp;amp;groupsize_beg+1 to n_last-&amp;amp;groupsize_end
        , &amp;amp;groupsize_beg to 1 by -1;
    set have point=ptr nobs=n_last;
    output;
  end;
  stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;or you can make it more compact via:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  ptr=ifn(_n_=1,n_last,ptr-1);
  if ptr=0 then stop;
  /*Read end group and begin group in reverse order, using "POINT=" */
  if _n_&amp;lt;=&amp;amp;groupsize_end  or  _n_&amp;gt; n_last-&amp;amp;groupsize_beg  then set have point=ptr nobs=n_last;
  /*Otherwise read middle group in original order */
  else set have (firstobs=%eval(&amp;amp;groupsize_beg+1));

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;
&lt;P&gt;The below is stricken out, once I realized you want the last batch of records in reverse order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;STRIKE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do i=1 to 9; output; end;
run;

%let groupsize_beg=3;
%let groupsize_end=3;

data want;
  do ptr= n_last to n_last +1 -&amp;amp;groupsize_end by -1
        , &amp;amp;groupsize_beg+1 to n_last-&amp;amp;groupsize_end
        , 1 to &amp;amp;groupsize_beg;
    set have point=ptr nobs=n_last;
    output;
  end;
  stop;
run;&lt;/CODE&gt;&lt;/STRIKE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRIKE&gt;or slightly more compact,&amp;nbsp; No STOP statement is required because the second and third SETs do not use the POINT= option:&lt;/STRIKE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;STRIKE&gt;&lt;CODE class=" language-sas"&gt;data want;
  ptr = n_last + 1 - _n_;

  if _n_&amp;lt;=&amp;amp;groupsize_end              then set have point=ptr nobs=n_last;
  else if _n_&amp;lt;= n_last-&amp;amp;groupsize_beg then set have (firstobs=%eval(&amp;amp;groupsize_beg+1));
  else                                     set have (obs=&amp;amp;groupsize_beg);
run;&lt;/CODE&gt;&lt;/STRIKE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRIKE&gt;&amp;nbsp;&lt;/STRIKE&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Dec 2023 01:03:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909537#M358727</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-23T01:03:01Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909552#M358739</link>
      <description>&lt;P&gt;You could consider a format to create a grouping for values. Many SAS Procs can return results ordered by formatted values without the need to pre-sort the input data.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input row_id;
  datalines;
1
2
3
4
5
6
7
8
9
;

proc format;
  value ValGroup
  7-9 = 1
  1-6 = 2
  other = 3
  ;
run;

proc sql;
/*  create table want as*/
  select row_id
  from have
  order by put(row_id,ValGroup.), row_id
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1703295202670.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/92043i49CFFE3CA06E4D36/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Patrick_0-1703295202670.png" alt="Patrick_0-1703295202670.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 23 Dec 2023 09:32:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/909552#M358739</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-23T09:32:20Z</dc:date>
    </item>
    <item>
      <title>Re: swap obs first and last each other</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/966610#M376187</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/390518"&gt;@pavank&lt;/a&gt;&amp;nbsp;does this answer your question?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data testy;
   input a;
   datalines;
1
2
3
4
5
6
7
8
9
;
run;
proc print data=testy;run;
proc sql noprint;
select distinct a
   into :avalue1-
   from testy;
quit;
%put &amp;amp;avalue1 &amp;amp;avalue9;
data testy2;
   set testy;
   if _n_=1 then a=&amp;amp;avalue9;
   if _n_=9 then a=&amp;amp;avalue1;
run;
proc print data=testy2;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_2-1747332182937.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106950i48EE877CEB50B6C0/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_2-1747332182937.png" alt="dxiao2017_2-1747332182937.png" /&gt;&lt;/span&gt;&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="dxiao2017_3-1747332235638.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/106951i6DF55654676A8D56/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_3-1747332235638.png" alt="dxiao2017_3-1747332235638.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 May 2025 18:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-obs-first-and-last-each-other/m-p/966610#M376187</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-05-15T18:04:39Z</dc:date>
    </item>
  </channel>
</rss>

