<?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 order multiple columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669293#M200756</link>
    <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data comes from proc transpose. the following data "have" is the first three columns, there are 200 columns after transpose, that is, COL1-COL200, each column is the itemid.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input COL1$ COL2$ COL3$;
cards;
p111 p777 p333
p444 p555 p666
p222 p222 p444
;
run;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to ascending order the itemid in each column, the "want" data is like following&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input COL1$ COL2$ COL3$;
cards;
p111 p222 p333
p222 p555 p444
p444 p777 p666
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;please help on the quick way of getting "want" data.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Tue, 14 Jul 2020 20:14:10 GMT</pubDate>
    <dc:creator>superbug</dc:creator>
    <dc:date>2020-07-14T20:14:10Z</dc:date>
    <item>
      <title>How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669293#M200756</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My data comes from proc transpose. the following data "have" is the first three columns, there are 200 columns after transpose, that is, COL1-COL200, each column is the itemid.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input COL1$ COL2$ COL3$;
cards;
p111 p777 p333
p444 p555 p666
p222 p222 p444
;
run;&amp;nbsp;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to ascending order the itemid in each column, the "want" data is like following&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input COL1$ COL2$ COL3$;
cards;
p111 p222 p333
p222 p555 p444
p444 p777 p666
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;please help on the quick way of getting "want" data.&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 20:14:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669293#M200756</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-07-14T20:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669295#M200757</link>
      <description>It's likely easier to do this before you transpose and then have it transposed in the desired order. What does the data look like prior to the TRANSPOSE? &lt;BR /&gt;Or my solution would be transpose, sort, transpose back.</description>
      <pubDate>Tue, 14 Jul 2020 20:17:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669295#M200757</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-14T20:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669301#M200760</link>
      <description>&lt;P&gt;You may need to provide more values as sorting of character values mixed with numeric elements may not sort in the order you want.&lt;/P&gt;
&lt;P&gt;Consider P111 and P6 (assume these are valid values). What order would you want for these?&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 20:44:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669301#M200760</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-14T20:44:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669302#M200761</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;&lt;/P&gt;
&lt;P&gt;Before transpose, my data looks like the following, each row is the item id a certain candidate takes. The first column "formname" is exam form name, "name_item1" to "name_item200" are the item ids.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have_before_trans;
input formname$ name_item1$ name_item2$ name_item3$;
cards;
AA01 p123 p321 p231
AA02 p245 p423 p543
AA03 p425 p326 p453
AA04 p235 p326 p423
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In each "formname", the order of of itemid in "name_item1" to "name_item200" are different. I want to order the itemids in "name_item1" to "name_item200" by "formname".&lt;/P&gt;
&lt;P&gt;please suggest what's the easier way to do that.&lt;/P&gt;
&lt;P&gt;Thanks much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 20:47:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669302#M200761</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-07-14T20:47:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669308#M200764</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;all of the variables are characters, so it should not have problem of "&lt;SPAN&gt;sorting of character values mixed with numeric elements' as you mentioned.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;my have data is like this&amp;nbsp; (there are 200 columns indicating item id, that is "name_item1" to "name_item200")&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input formname$ name_item1$ name_item2$ name_item3$;
cards;
AA01 p111 p444 p222
AA02 p444 p111 p444
AA01 p222 p222 p333
AA02 p333 p333 p111
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;data i want is like the following&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input formname$ name_item1$ name_item2$ name_item3$;
cards;
AA01 p111 p222 p222
AA01 p222 p444 p333
AA02 p333 p111 p111
AA02 p444 p333 p444
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Please suggest. Thank you so much!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 21:12:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669308#M200764</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-07-14T21:12:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669323#M200770</link>
      <description>Do a CALL SORT() on the array and then transpose.&lt;BR /&gt;&lt;BR /&gt;data temp / view=temp;&lt;BR /&gt;set have_before_trans;&lt;BR /&gt;array _ni(*) name_item1-name_item3;&lt;BR /&gt;call sortc(of _ni(*));&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Then do your transpose. If you have missings in there it may not work as missings get sorted to the beginning.</description>
      <pubDate>Tue, 14 Jul 2020 22:00:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669323#M200770</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-07-14T22:00:45Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669326#M200772</link>
      <description>&lt;P&gt;I hope your are right about the sort issue.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This works for the example provided data.&lt;/P&gt;
&lt;PRE&gt;data have;
input formname$ name_item1$ name_item2$ name_item3$;
cards;
AA01 p111 p444 p222
AA02 p444 p111 p444
AA01 p222 p222 p333
AA02 p333 p333 p111
;
run;
data temp;
   set have;
   array n name_:;
   length item $ 5;
   do i= 1 to dim(n);
      item = n[i];
      output;
   end;
   keep formname item;
run;

proc sort data=temp;
   by formname item;
run;

data temp2;
   set temp;
   /* the magin number 3 is the original number of 
      name_item variables in the starting data
      replace ALL of the 3 with that number
   */
   array name_item(3) $ 4;
   retain name_: ;
   index = mod(_n_,3)+(3*(mod(_n_,3)=0)) ;
   name_item[index]=item;
   if index=3 then output;
   keep formname name_: ;
run;
&lt;/PRE&gt;
&lt;P&gt;Caveat: If you try to make the output data with a different number of output name_item variables the numbering of the sequences calculating the Index may become quite obnoxious and could have issues about the "odd" values. Such as creating 3 output columns with 10 values. Additional code would be needed to address the "last" record to create the output record with only the first name_item set.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 22:09:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669326#M200772</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-14T22:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669351#M200782</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code worked for the example data. Thanks much!&lt;/P&gt;
&lt;P&gt;After applying the code in my real data, I realized "name_item" need to be ordered by blocks, that is,&lt;/P&gt;
&lt;P&gt;block1: name_item1-name_item50&lt;/P&gt;
&lt;P&gt;block2: name_item51-name_item100&lt;/P&gt;
&lt;P&gt;block3: name_item101-name_item150&lt;/P&gt;
&lt;P&gt;block4: name_item151-name_item200&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help modify your "data&amp;nbsp; temp2" step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 02:08:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669351#M200782</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-07-15T02:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669354#M200783</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My real data has 200 items, and "divided" into 4 blocks as following&lt;/P&gt;
&lt;P&gt;block 1: name_item1-name_item50&lt;/P&gt;
&lt;P&gt;block 2: name_item51-name_item100&lt;/P&gt;
&lt;P&gt;block 3: name_item101-name_item150&lt;/P&gt;
&lt;P&gt;block 4: name_item151-name_item200&lt;/P&gt;
&lt;P&gt;When administer the test, the order of item name (name_item:) is different &lt;STRONG&gt;within each block&amp;nbsp;&lt;/STRONG&gt;for different candidates taking the &lt;STRONG&gt;same&lt;/STRONG&gt; form_name. My purpose is after reordering item name, for candidates taking the &lt;STRONG&gt;same form_name, the order of item name are the same for all 200 items&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In my real data, I added candidate&lt;STRONG&gt; id&lt;/STRONG&gt; as in the following code&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
   set have;
   array n name_:;
   length item $ 10;
   do i= 1 to dim(n);
      item = n[i];
      output;
   end;
   keep formname id item;
run;

proc sort data=temp;
   by formname id item;
run;

data temp2;
   set temp;
/* the magin number 3 is the original number of 
      name_item variables in the starting data
      replace ALL of the 3 with that number
   */
   array name_item(200) $ 10;
   retain name_: ;
   index = mod(_n_,200)+(200*(mod(_n_,200)=0)) ;
   name_item[index]=item;
   if index=200 then output;
   keep form_name id name_: ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In "temp2" data I got,&amp;nbsp; item order in the first block, that is, "name_item1" to "name_item50" are the same for &lt;STRONG&gt;all test take&lt;/STRONG&gt;r &lt;STRONG&gt;and&lt;/STRONG&gt;&amp;nbsp;&lt;STRONG&gt;across&lt;/STRONG&gt;&lt;STRONG&gt;&amp;nbsp;all form_name&lt;/STRONG&gt;, which is desired. But the item order of&amp;nbsp; the rest three blocks are not&amp;nbsp; quite what I wanted, in other words, item order in the 2nd block ( "name_item51" to "name_item100") are the &lt;STRONG&gt;same within each block but different across form_name&lt;/STRONG&gt;. Same results to the 3rd and 4th blocks.&lt;/P&gt;
&lt;P&gt;Could you please modify your code to help get the desired results I wanted.&lt;/P&gt;
&lt;P&gt;Thank you!!&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 03:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669354#M200783</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-07-15T03:03:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669476#M200834</link>
      <description>&lt;P&gt;Here is code which gives you the WANT data from the HAVE as you have shown, using a "hash of hashes" (the HoH object):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input formname$ name_item1$ name_item2$ name_item3$;
cards;
AA01 p111 p444 p222
AA02 p444 p111 p444
AA01 p222 p222 p333
AA02 p333 p333 p111
;
run;

proc sort data=have;
  by formname;
run;


data want;
  set have;
  by formname;
  array cols(*) name_item:;
  
  retain index 0;
  if _N_=1 then do;
    declare hash h;
    declare hiter hit;
    
    declare hash HoH(ordered: 'Y');
    HoH.definekey('index');
    HoH.definedata('h','hit');
    HoH.definedone();
    
    do index=1 to dim(cols);
      h=_new_ hash(ordered:'Y', multidata:'Y');
      h.definekey(vname(cols(index)));
      h.definedata(vname(cols(index)));
      h.definedone();
      hit=_new_ hiter('h');
      HoH.add();
      end;
    end;
    do index=1 to dim(cols);
      HoH.find();
      h.add();
      end;
    done=0;
    if last.formname then do until(0);
      do index=1 to dim(cols);
        HoH.find();
        done=hit.next();
        if done then
          h.clear();
        end;
      if done then leave;
      output;
      end;
  drop done index;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I do not understand what you are writing about blocks of 50 columns - could you show some sample data, or explain more specifically?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 14:32:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669476#M200834</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2020-07-15T14:32:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669483#M200837</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/304931"&gt;@superbug&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code worked for the example data. Thanks much!&lt;/P&gt;
&lt;P&gt;After applying the code in my real data, I realized "name_item" need to be ordered by blocks, that is,&lt;/P&gt;
&lt;P&gt;block1: name_item1-name_item50&lt;/P&gt;
&lt;P&gt;block2: name_item51-name_item100&lt;/P&gt;
&lt;P&gt;block3: name_item101-name_item150&lt;/P&gt;
&lt;P&gt;block4: name_item151-name_item200&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help modify your "data&amp;nbsp; temp2" step?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Provide actual example data. You have now changed the input data structure at least twice.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instructions here: &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712&lt;/A&gt; will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the &amp;lt;/&amp;gt; icon or attached as text to show exactly what you have and that we can test code against. &lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 14:38:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669483#M200837</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-07-15T14:38:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to order multiple columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669668#M200915</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/76464"&gt;@s_lassen&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your help and suggestions!&lt;/P&gt;
&lt;P&gt;I figured out some other ways to get what I wanted.&lt;/P&gt;
&lt;P&gt;Thanks to all of your experts! I am learning how to make myself clear when I ask help in this forum.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jul 2020 19:28:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-order-multiple-columns/m-p/669668#M200915</guid>
      <dc:creator>superbug</dc:creator>
      <dc:date>2020-07-15T19:28:30Z</dc:date>
    </item>
  </channel>
</rss>

