<?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 rows  based on name column in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974582#M377940</link>
    <description>&lt;P&gt;i tried&amp;nbsp; &amp;nbsp;i want swap two id's for each name keep last&amp;nbsp; name id as it is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inputs;
input name $ id;
datalines;
A 1
B 6
C 3
D 9
E 5
F 6
G 7
H 8
I 9
;
run;

/* Swap IDs using arrays */
data want;
   array n[100] $ _temporary_;   /* to hold names */
   array x[100]   _temporary_;   /* to hold ids   */

   do i=1 by 1 until (lastrow);
      set inputs end=lastrow;
      n[i] = name;
      x[i] = id;
   end;

   /* Swap in pairs */
   do j=1 to i by 2;
      if j+1 &amp;lt;= i then do;
         temp   = x[j];
         x[j]   = x[j+1];
         x[j+1] = temp;
      end;
   end;

   /* Output final dataset */
   do k=1 to i;
      name = n[k];
      id   = x[k];
      output;
   end;

   keep name id;
run;

proc print data=want noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 09 Sep 2025 09:04:54 GMT</pubDate>
    <dc:creator>BrahmanandaRao</dc:creator>
    <dc:date>2025-09-09T09:04:54Z</dc:date>
    <item>
      <title>swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974579#M377937</link>
      <description>&lt;P&gt;Hi Experts&lt;/P&gt;
&lt;P&gt;Here&amp;nbsp; I have a question i want&amp;nbsp; swap row based on name variable&amp;nbsp; how to solve if it huge data could you please give solution&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inputs;
input name $ id ;
datalines ;
A 1
B 6
C 3
D 9
E 5
F 6
G 7
;
run;


/*Required output*/

Name id

A 6
B 1
C 9
D 3
E 6
F 5
G 7

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Sep 2025 08:39:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974579#M377937</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2025-09-09T08:39:37Z</dc:date>
    </item>
    <item>
      <title>Re: swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974580#M377938</link>
      <description>&lt;P&gt;I don't understand the logic here. Please be more specific.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2025 08:54:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974580#M377938</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2025-09-09T08:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974582#M377940</link>
      <description>&lt;P&gt;i tried&amp;nbsp; &amp;nbsp;i want swap two id's for each name keep last&amp;nbsp; name id as it is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inputs;
input name $ id;
datalines;
A 1
B 6
C 3
D 9
E 5
F 6
G 7
H 8
I 9
;
run;

/* Swap IDs using arrays */
data want;
   array n[100] $ _temporary_;   /* to hold names */
   array x[100]   _temporary_;   /* to hold ids   */

   do i=1 by 1 until (lastrow);
      set inputs end=lastrow;
      n[i] = name;
      x[i] = id;
   end;

   /* Swap in pairs */
   do j=1 to i by 2;
      if j+1 &amp;lt;= i then do;
         temp   = x[j];
         x[j]   = x[j+1];
         x[j+1] = temp;
      end;
   end;

   /* Output final dataset */
   do k=1 to i;
      name = n[k];
      id   = x[k];
      output;
   end;

   keep name id;
run;

proc print data=want noobs; run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Sep 2025 09:04:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974582#M377940</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2025-09-09T09:04:54Z</dc:date>
    </item>
    <item>
      <title>Re: swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974585#M377941</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inputs;
input name $ id ;
datalines ;
A 1
B 6
C 3
D 9
E 5
F 6
G 7
;
run;

data want;
 merge inputs inputs(keep=id rename=(id=_id) firstobs=2);
 lag_id=lag(id);
 if mod(_n_,2)=0 then id=lag_id;
  else id=coalesce(_id,id);
 drop _id lag_id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Sep 2025 09:30:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974585#M377941</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-09T09:30:01Z</dc:date>
    </item>
    <item>
      <title>Re: swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974587#M377943</link>
      <description>&lt;P&gt;Hi Sharp&lt;/P&gt;
&lt;P&gt;Thank you&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Brilliant Answer&amp;nbsp; can we achieve this in proc sql&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2025 09:48:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974587#M377943</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2025-09-09T09:48:21Z</dc:date>
    </item>
    <item>
      <title>Re: swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974591#M377944</link>
      <description>&lt;P&gt;Sure. Of course.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inputs;
input name $ id ;
datalines ;
A 1
B 6
C 3
D 9
E 5
F 6
G 7
;
run;

data temp;
set inputs;
n+1;
run;
proc sql;
create table want as
select name,
case when mod(n,2)=1 then (select id from temp where n=min(a.n+1,(select count(*) from temp))) 
 else (select id from temp where n=a.n-1)
end as id
 from temp as a; 
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Sep 2025 10:01:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974591#M377944</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-09-09T10:01:53Z</dc:date>
    </item>
    <item>
      <title>Re: swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974594#M377945</link>
      <description>&lt;P&gt;Hi Sharp,&lt;/P&gt;
&lt;P&gt;Thank you very much for your proc sql solution i am impressed your solutions&lt;span class="lia-unicode-emoji" title=":ok_hand:"&gt;👌&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2025 10:06:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974594#M377945</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2025-09-09T10:06:01Z</dc:date>
    </item>
    <item>
      <title>Re: swap rows  based on name column</title>
      <link>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974620#M377956</link>
      <description>&lt;P&gt;You seem to be missing a variable that would indicate which observations should be pared.&amp;nbsp; And also which member of the pair they are.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could create one when you read in the data (or afterwords).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data inputs;
  pair = int((_n_+1)/2);
  order= mod(_n_+1,2);
  input name $ id @@ ;
datalines ;
A 1 B 6
C 3 D 9
E 5 F 6
G 7
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now it is simple to perform your name swap.&amp;nbsp; Just make a version of the data with the order of the members of the pair reversed and then merge them back together.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=inputs out=reverse;
  by pair descending order ;
run;

data want ;
  merge inputs reverse(keep=pair name);
  by pair;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1757423975317.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/109798i4EE8E99A839592D7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1757423975317.png" alt="Tom_0-1757423975317.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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2025 13:19:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/swap-rows-based-on-name-column/m-p/974620#M377956</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-09-09T13:19:48Z</dc:date>
    </item>
  </channel>
</rss>

