<?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 do you randomly and independently sort multiple columns? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-randomly-and-independently-sort-multiple-columns/m-p/815500#M321895</link>
    <description>&lt;UL&gt;
&lt;LI&gt;Transpose the data so that the columns are stacked, to a long data set&lt;/LI&gt;
&lt;LI&gt;Sort once using the random technique&lt;/LI&gt;
&lt;LI&gt;Add in counter for transpose/merge back&lt;/LI&gt;
&lt;LI&gt;Sort for transpose&lt;/LI&gt;
&lt;LI&gt;Transpose back to wide format&lt;/LI&gt;
&lt;LI&gt;Merge back with ID variables&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=' ';
input Obs : 32. ID :$8. Z1 :32. Z2 :32. Z3 :32.;
datalines;
1 A 5 5 5
2 B 7.5 7.5 7.5
3 C 6.123 6.123 6.123
4 D 8 8 8
5 E 1 1 1
;
run;

proc transpose data=have out=long;
by obs id;
run;

data random;
set long;
num = rand('normal',0,1);
run;

proc sort data=random (drop=obs);
by   _name_ num;
run;

data random2;
set random;
by _name_;
if first._name_ then obs=1;
else obs+1;
run;

proc sort data=random2; by obs;

proc transpose data=random2 out=wide2;
by obs;
var col1 ;
id _name_;
run;

data want;
merge have (keep=id obs) wide2;
by obs;
drop _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 27 May 2022 21:34:55 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2022-05-27T21:34:55Z</dc:date>
    <item>
      <title>How do you randomly and independently sort multiple columns?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-randomly-and-independently-sort-multiple-columns/m-p/815497#M321893</link>
      <description>&lt;P&gt;Suppose I have the following data (in reality there are many more observations &amp;amp; the Z1, Z2, etc variables go to Z1000):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input Obs:32 ID:$8. Z1:32. Z2:32. Z3:32.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 A 5 5 5&lt;BR /&gt;2 B 7.5 7.5 7.5&lt;BR /&gt;3 C 6.123 6.123 6.123&lt;BR /&gt;4 D 8 8 8&lt;BR /&gt;5 E 1 1 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only thing I want to do is randomly sort each Z column, but importantly all of these sorts have to be independent of each other. An example of the ending dataset I would want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 A&amp;nbsp; &amp;nbsp; 8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6.123&lt;BR /&gt;2 B&amp;nbsp; &amp;nbsp; 6.123&amp;nbsp; &amp;nbsp; 7.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&lt;BR /&gt;3 C&amp;nbsp; &amp;nbsp; 7.5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;BR /&gt;4 D&amp;nbsp; &amp;nbsp; 5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6.123&amp;nbsp; &amp;nbsp; 8&lt;BR /&gt;5 E&amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;8&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;7.5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If there were in fact only 3 'Z' columns I would just do the below:&lt;/P&gt;&lt;P&gt;data have2;&lt;/P&gt;&lt;P&gt;set have (keep =&amp;nbsp; Z1);&lt;/P&gt;&lt;P&gt;num = rand('normal',0,1);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then sort on num, create a count variable _N_ which I will use to left join back to the obs variable, then repeat twice more. However, given I have thousands of columns, this brute force method is not appealing. I am probably overlooking something. Any ideas?&lt;/P&gt;</description>
      <pubDate>Fri, 27 May 2022 21:14:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-randomly-and-independently-sort-multiple-columns/m-p/815497#M321893</guid>
      <dc:creator>Jacob3</dc:creator>
      <dc:date>2022-05-27T21:14:05Z</dc:date>
    </item>
    <item>
      <title>Re: How do you randomly and independently sort multiple columns?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-randomly-and-independently-sort-multiple-columns/m-p/815500#M321895</link>
      <description>&lt;UL&gt;
&lt;LI&gt;Transpose the data so that the columns are stacked, to a long data set&lt;/LI&gt;
&lt;LI&gt;Sort once using the random technique&lt;/LI&gt;
&lt;LI&gt;Add in counter for transpose/merge back&lt;/LI&gt;
&lt;LI&gt;Sort for transpose&lt;/LI&gt;
&lt;LI&gt;Transpose back to wide format&lt;/LI&gt;
&lt;LI&gt;Merge back with ID variables&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dlm=' ';
input Obs : 32. ID :$8. Z1 :32. Z2 :32. Z3 :32.;
datalines;
1 A 5 5 5
2 B 7.5 7.5 7.5
3 C 6.123 6.123 6.123
4 D 8 8 8
5 E 1 1 1
;
run;

proc transpose data=have out=long;
by obs id;
run;

data random;
set long;
num = rand('normal',0,1);
run;

proc sort data=random (drop=obs);
by   _name_ num;
run;

data random2;
set random;
by _name_;
if first._name_ then obs=1;
else obs+1;
run;

proc sort data=random2; by obs;

proc transpose data=random2 out=wide2;
by obs;
var col1 ;
id _name_;
run;

data want;
merge have (keep=id obs) wide2;
by obs;
drop _name_;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 May 2022 21:34:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-randomly-and-independently-sort-multiple-columns/m-p/815500#M321895</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-05-27T21:34:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do you randomly and independently sort multiple columns?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-randomly-and-independently-sort-multiple-columns/m-p/815504#M321897</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;, I should've come up with this myself :(. Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 27 May 2022 21:40:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-randomly-and-independently-sort-multiple-columns/m-p/815504#M321897</guid>
      <dc:creator>Jacob3</dc:creator>
      <dc:date>2022-05-27T21:40:42Z</dc:date>
    </item>
  </channel>
</rss>

