<?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: Changing column order after combining multiple transposes in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688337#M209075</link>
    <description>An ATTRIB statement also works.</description>
    <pubDate>Thu, 01 Oct 2020 20:04:45 GMT</pubDate>
    <dc:creator>CurtisMackWSIPP</dc:creator>
    <dc:date>2020-10-01T20:04:45Z</dc:date>
    <item>
      <title>Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688334#M209073</link>
      <description>&lt;P&gt;I am attempting to transform a dataset that has the following layout:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ; 
input ID type$ baseline peak diff ; 
cards ; 
1 A 5 6 1 
1 B 7 4 -3 
2 A 12 3 -9
2 B 3 3 0 
3 A 4 8 4 
3 B 1 8 7 
; 
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Using the following code:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=baseline prefix=baseline ; 
by ID ; 
ID type ; 
var baseline ; 
run;

proc transpose data=have out=peak prefix=peak ; 
by ID ; 
ID type ; 
var peak; 
run ;

proc transpose data=have out=diff prefix=diff ; 
by ID ; 
ID type ; 
var diff ; 
run ;

data combined ; 
merge baseline peak diff ; 
by ID ; 
run ;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The resulting dataset has this format:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data combined ; 
input ID baselineA baselineB peakA peakB diffA diffB; 
cards ; 
1 5 7 6 4 1 -3 
2 12 3 3 3 -9 0 
3 4 1 8 8 4 7 &lt;BR /&gt;; 
run ; &lt;/PRE&gt;&lt;P&gt;This is mostly the format I want, however I need the variables in a slightly different order, as follows:&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ; 
	input ID baselineA peakA diffA baselineB peakB diffB ; 
	cards ; 
	1 5 6 1 7 4 -3 
	2 12 3 -9 3 3 0 
	3 4 8 4 1 8 7 
	; 
run ; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Basically, I variables for the same type (A,B) from each transpose listed sequentially.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One thing to note is that with the above example I could easily just reorder the variables using the retain statement. However, in my actual dataset rather than just having two types (A and B), there are 31 categories, and there are 4 rather than 3 transposes. This means 124 (31x4) columns to reorder, which would be a bit tedious.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What is the most efficient way to reorder the columns like I want? Is there a better method than transposing separately 3 times?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Oct 2020 19:59:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688334#M209073</guid>
      <dc:creator>tmcwill</dc:creator>
      <dc:date>2020-10-01T19:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688336#M209074</link>
      <description>&lt;P&gt;Declare them in the order you need before the input, set, or merge statement.&amp;nbsp; A length or format statement will work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;length ID baselineA peakA diffA baselineB peakB diffB 8;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Oct 2020 20:03:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688336#M209074</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-10-01T20:03:06Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688337#M209075</link>
      <description>An ATTRIB statement also works.</description>
      <pubDate>Thu, 01 Oct 2020 20:04:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688337#M209075</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-10-01T20:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688354#M209085</link>
      <description>&lt;P&gt;Thanks for the response! My issue with this is that there are 124 variables, and I don't want to have to type 124 variables in the length/retain/attribute statement.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 01 Oct 2020 20:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688354#M209085</guid>
      <dc:creator>tmcwill</dc:creator>
      <dc:date>2020-10-01T20:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688362#M209091</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302153"&gt;@tmcwill&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the response! My issue with this is that there are 124 variables, and I don't want to have to type 124 variables in the length/retain/attribute statement.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Consider this example;&lt;/P&gt;
&lt;PRE&gt;data have ; 
input ID type$ baseline peak diff ; 
cards ; 
1 A 5 6 1 
1 B 7 4 -3 
2 A 12 3 -9
2 B 3 3 0 
3 A 4 8 4 
3 B 1 8 7 
; 
run ;
proc transpose data=have out=trans;
   by id type;
run;

proc transpose data=trans out=trans2;
   by id;
   id _name_ type;
   var col1;
run;&lt;/PRE&gt;
&lt;P&gt;And will save doing a bunch of transposes as well.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Oct 2020 20:46:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688362#M209091</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-01T20:46:36Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688371#M209096</link>
      <description>&lt;P&gt;Then my response is why do you care what order they are in?&amp;nbsp; You can fix the order in whatever output process you use.&lt;/P&gt;
&lt;P&gt;How would the system know what order you want until you tell it?&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302153"&gt;@tmcwill&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the response! My issue with this is that there are 124 variables, and I don't want to have to type 124 variables in the length/retain/attribute statement.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Thu, 01 Oct 2020 21:24:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688371#M209096</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-10-01T21:24:53Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688372#M209097</link>
      <description>&lt;P&gt;Or, write a little MACRO to save some typing.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Oct 2020 21:26:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688372#M209097</guid>
      <dc:creator>CurtisMackWSIPP</dc:creator>
      <dc:date>2020-10-01T21:26:09Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688377#M209098</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/339357"&gt;@CurtisMackWSIPP&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Then my response is why do you care what order they are in?&amp;nbsp; You can fix the order in whatever output process you use.&lt;/P&gt;
&lt;P&gt;How would the system know what order you want until you tell it?&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302153"&gt;@tmcwill&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the response! My issue with this is that there are 124 variables, and I don't want to have to type 124 variables in the length/retain/attribute statement.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;/BLOCKQUOTE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I've given up on asking the "what difference does the order make". To many Excel contaminated brains.&lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My response was basically to get rid of the multiple proc transpose calls in the spirit of efficiency and just happens to address the order issue.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 15:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688377#M209098</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-02T15:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688550#M209188</link>
      <description>&lt;P&gt;Thanks, this is great! I figured there had to be a more efficient way to do this than doing each transpose individually, but I couldn't quite find what I was looking for online. And as you mentioned, it also fixes the variable order issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thanks again!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 15:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688550#M209188</guid>
      <dc:creator>tmcwill</dc:creator>
      <dc:date>2020-10-02T15:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688551#M209189</link>
      <description>&lt;P&gt;Hi, thanks again for your responses. Just an FYI, normally I don't care what order variables are in for my own analyses, but I am doing this for another researcher who needs the data in a very specific format in order to use the dataset as an input in an exterior program that will generate a complex figure/plot.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 15:25:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688551#M209189</guid>
      <dc:creator>tmcwill</dc:creator>
      <dc:date>2020-10-02T15:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Changing column order after combining multiple transposes</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688636#M209228</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/302153"&gt;@tmcwill&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi, thanks again for your responses. Just an FYI, normally I don't care what order variables are in for my own analyses, but I am doing this for another researcher who needs the data in a very specific format in order to use the dataset as an input in an exterior program that will generate a complex figure/plot.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So the OUTPUT has to be in a specific order. Since I haven't run into any SAS procedures that are picky I have to assume that you would be talking about a text file or such in a specific order.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have done a lot of that, to include specifying rows and columns on a page. Data step and put statements work for that.&lt;/P&gt;
&lt;P&gt;Or having the variable names on a VAR statement in Proc Print.&lt;/P&gt;
&lt;P&gt;A multitude of like named, or grouped variables could be done with a data step to create&amp;nbsp; long string value that you then stuff into a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data example;
   length longstring $ 5000;
   varlist= "thisvar thatvar othervar";
   /* suppose we want to creat 15 grouped variable names
      space delimited in a long string*/
   do i= 1 to 15; /*the number of groups*/
      do j=1 to countw(varlist);
         longstring = catx(' ',longstring,cats(scan(varlist,j),i));
      end;
   end;
   call symputx('myvarlist',strip(longstring));
run;

%put the longstring is: &amp;amp;myvarlist.;
&lt;/PRE&gt;
&lt;P&gt;Then you could use the macro variable myvarlist such as in&lt;/P&gt;
&lt;PRE&gt;Proc print data=somedataset;
   var &amp;amp;myvarlist. ;
run;&lt;/PRE&gt;
&lt;P&gt;or any other place you want that list of variables.&lt;/P&gt;</description>
      <pubDate>Fri, 02 Oct 2020 23:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Changing-column-order-after-combining-multiple-transposes/m-p/688636#M209228</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-10-02T23:25:22Z</dc:date>
    </item>
  </channel>
</rss>

