<?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 values using PROC SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332705#M272055</link>
    <description>I love (sarcasm) when the poster "I don't want to use X". What if X is the best solution?&lt;BR /&gt;And then I wonder about the requirement. What kind of data is this, no unique id.? What's the purpose?</description>
    <pubDate>Tue, 14 Feb 2017 16:47:04 GMT</pubDate>
    <dc:creator>LinusH</dc:creator>
    <dc:date>2017-02-14T16:47:04Z</dc:date>
    <item>
      <title>Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332681#M272054</link>
      <description>&lt;P&gt;I have a dataset say "A" and I want to swap each value of "A" using proc sql.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data a;&lt;BR /&gt;input x$;&lt;BR /&gt;cards;&lt;BR /&gt;a&lt;BR /&gt;b&lt;BR /&gt;a&lt;BR /&gt;b&lt;BR /&gt;a&lt;BR /&gt;b&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I would need output as&amp;nbsp;&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I dont want to use any function here. Inputs anyone?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 16:10:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332681#M272054</guid>
      <dc:creator>adityaa9z</dc:creator>
      <dc:date>2017-02-14T16:10:35Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332705#M272055</link>
      <description>I love (sarcasm) when the poster "I don't want to use X". What if X is the best solution?&lt;BR /&gt;And then I wonder about the requirement. What kind of data is this, no unique id.? What's the purpose?</description>
      <pubDate>Tue, 14 Feb 2017 16:47:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332705#M272055</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2017-02-14T16:47:04Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332707#M272056</link>
      <description>&lt;P&gt;Haha. I understand your problem. though this was the question asked in one of my interviews. I responded that I can do that with functions. they denied.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yeah this data doesnt have any unique id. I just want to swap every value with the one below it. Hope this makes sense.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 16:50:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332707#M272056</guid>
      <dc:creator>adityaa9z</dc:creator>
      <dc:date>2017-02-14T16:50:49Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332711#M272057</link>
      <description>&lt;P&gt;What do you mean by function? &amp;nbsp;SAS specific functions? &amp;nbsp;My first thought would be that you could do this with a CASE statement if that is allowable and you knew all of the swaps that need to happen ahead of time. &amp;nbsp;I don't think you could solve this with just SELECTs and JOINs, although I'd love to be proved wrong.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 16:56:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332711#M272057</guid>
      <dc:creator>Sven111</dc:creator>
      <dc:date>2017-02-14T16:56:35Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332717#M272058</link>
      <description>&lt;P&gt;No functions and Proc sql:&lt;/P&gt;
&lt;PRE&gt;proc sql;
   create table want as 
   select *
   from a(firstobs=2) union all (select * from a(firstobs=1 obs=1))
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;would not work for more than two values or data that wasn't specifically ordered as the example.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could also use a custom format with something like select put(x,myfmt.) as x&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 17:03:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332717#M272058</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-02-14T17:03:34Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332725#M272059</link>
      <description>&lt;P&gt;Next code will do it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; create table B as select&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; case when x="A" then "B"&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;when x="B" then "A"&lt;BR /&gt;&amp;nbsp; &amp;nbsp;end as x&lt;/P&gt;
&lt;P&gt;from A;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 17:16:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332725#M272059</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2017-02-14T17:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332729#M272060</link>
      <description>&lt;P&gt;Hi.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That's an awkward problem &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let pairs=2;
proc sql noprint;
create table want as
select monotonic() as pos, x from a
order by (int((calculated pos-1)/&amp;amp;PAIRS)*&amp;amp;PAIRS)+mod(calculated pos,&amp;amp;PAIRS);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I got this right, one approach would be to number the observations and then do some math over that to reorder the pairs&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;int(pos-1)/&amp;amp;PAIRS + mod(pos,&amp;amp;PAIRS)&lt;BR /&gt;&lt;BR /&gt;First part caculates the base value (if PAIRS=2, then 0, 2, 4, 6, ...)&amp;nbsp;&lt;BR /&gt;Second part creates a rotating inde (if PAIRS=2, then +1, +0, +1, +0, ...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can drop the calculated pos variable if you want, I just left it there for understanding.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope it helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Daniel Santos&amp;nbsp;@ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Feb 2017 17:35:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332729#M272060</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2017-02-14T17:35:59Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332750#M272061</link>
      <description>Thanks Daniel. This works. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Tue, 14 Feb 2017 18:19:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/332750#M272061</guid>
      <dc:creator>adityaa9z</dc:creator>
      <dc:date>2017-02-14T18:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Swap values using PROC SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/435278#M272062</link>
      <description>&lt;P&gt;This code doesn't work if there is a consecutive occurrence of same value in the list.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;like&amp;nbsp;&lt;/P&gt;&lt;P&gt;x&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;a&lt;/P&gt;&lt;P&gt;b&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Simple case statement in proc sql query will fix it.&amp;nbsp;&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;proc sql;
create table want as 
select case x when 'a' then 'b'
				when 'b' then 'a'
				end as x
	from a ;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Feb 2018 15:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Swap-values-using-PROC-SQL/m-p/435278#M272062</guid>
      <dc:creator>mohdsheikibrahi</dc:creator>
      <dc:date>2018-02-08T15:02:28Z</dc:date>
    </item>
  </channel>
</rss>

