<?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: RE: PROC TRANSPOSE with correct columns order in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263730#M51628</link>
    <description>&lt;P&gt;you can always change the order of variables after proc transpose .&lt;/P&gt;
&lt;P&gt;Use SQL select the right order variable name and Use RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;select distinct cats('_',year) into : list separated by ' '&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;from have&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;order by year;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;..........&lt;/P&gt;
&lt;P&gt;proc transpose out=want;.........&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data final;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;retain Gender &amp;amp;list Total;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;set want;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Thu, 14 Apr 2016 02:24:21 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2016-04-14T02:24:21Z</dc:date>
    <item>
      <title>RE: PROC TRANSPOSE with correct columns order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263512#M51575</link>
      <description>&lt;P&gt;Hi....I am trying to get the output to order the columns that have been transposed. It seems the problem that I am having is that because not all ID's have data for each YrMonth, and since the data is sorted by ID and then YrMonth, the output and columns begin withthe results from the first ID. Any suggestions how I can remedy this so that the first transposed column is the first ordered YrMonth....Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SORT DATA=NEW1;&lt;BR /&gt;BY NAME ID YRMONTH;&lt;BR /&gt;RUN;&lt;/P&gt;
&lt;P&gt;PROC TRANSPOSE DATA=NEW1 OUT=WANT;&lt;BR /&gt;BY NAME ID;&lt;BR /&gt;ID YRMONTH;&lt;BR /&gt;VAR COST;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 13:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263512#M51575</guid>
      <dc:creator>twildone</dc:creator>
      <dc:date>2016-04-13T13:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: RE: PROC TRANSPOSE with correct columns order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263517#M51577</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please don't code all in uppercase - it is very difficult to read. &amp;nbsp;Also, providing test data in the form of a datastep will yield you tested code. &amp;nbsp;So what your saying is you end up with something like:&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp;_201501 &amp;nbsp;_201506 &amp;nbsp;_201504...&lt;/P&gt;
&lt;P&gt;Yes?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If so then maybe you could create a shell dataset, and then set the transposed data back to that -&lt;/P&gt;
&lt;PRE&gt;data new1;
  name="abc"; id=1; yrmonth=201501; cost=34; output;
  name="abc"; id=1; yrmonth=201505; cost=.; output;
  name="abc"; id=1; yrmonth=201506; cost=54; output;
run;
proc sql; 
  create table TEMP (_201501 num,_201505 num,_201506 num);
run;
proc sort data=new1;
  by name id yrmonth;
run;
proc transpose data=new1 out=want;
  by name id;
  id yrmonth;
  var cost;
run;
data want;
  set temp want;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 14:15:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263517#M51577</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-04-13T14:15:48Z</dc:date>
    </item>
    <item>
      <title>Re: RE: PROC TRANSPOSE with correct columns order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263526#M51580</link>
      <description>&lt;P&gt;Forum genius Tom helped me with this yesterday...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One method is to create a dummy set of records that has all of the dates in the order that you want so that PROC TRANSPOSE will see those dates first. Right now it is seeing them in the order that they appear after the data has first been sorted. &amp;nbsp;You can then drop that resulting row from the output dataset.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sort&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;FinalData &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;keep&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;ad_dt&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; out&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;dates nodupkey&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; ad_dt&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; both &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;set&lt;/SPAN&gt; dates finalData &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;transpose&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;both out&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;Want&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;drop&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;_name_ &lt;SPAN class="token statement"&gt;where&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;rule_nm &lt;SPAN class="token operator"&gt;ne&lt;/SPAN&gt; &lt;SPAN class="token string"&gt;' '&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; rule_nm rule_order&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt; ad_dt_char&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;n&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc transpose creates the columns as it sees new values of the ID variable. So by putting at least one example of every possible date in the first BY group (and in the right order) then it creates the columns in the right order. &amp;nbsp;Then the WHERE= dataset option deletes the extra group from the output. &amp;nbsp;Consider a &amp;nbsp;simple example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; have &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
   &lt;SPAN class="token keyword"&gt;input&lt;/SPAN&gt; gender &lt;SPAN class="token punctuation"&gt;$&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;count&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;@&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;@&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token datalines"&gt;&lt;SPAN class="token keyword"&gt;cards&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token data string"&gt;F 2003 10 F 2005 11 M 2002 12 M 2003 13&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;transpose&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;have out&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;want &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; gender &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;year&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
  &lt;SPAN class="token keyword"&gt;var&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;count&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;
&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;

&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This will result in columns GENDER _2003 _2005 _2002.&lt;/P&gt;&lt;P&gt;But if I add an extra set of records that before the F group that has YEAR in the order 2002,2003,2005 then the columns in the output dataset will be created in that order.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Apr 2016 14:26:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263526#M51580</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2016-04-13T14:26:08Z</dc:date>
    </item>
    <item>
      <title>Re: RE: PROC TRANSPOSE with correct columns order</title>
      <link>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263730#M51628</link>
      <description>&lt;P&gt;you can always change the order of variables after proc transpose .&lt;/P&gt;
&lt;P&gt;Use SQL select the right order variable name and Use RETAIN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;select distinct cats('_',year) into : list separated by ' '&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;from have&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;order by year;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;..........&lt;/P&gt;
&lt;P&gt;proc transpose out=want;.........&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data final;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;retain Gender &amp;amp;list Total;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;set want;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2016 02:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/RE-PROC-TRANSPOSE-with-correct-columns-order/m-p/263730#M51628</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-04-14T02:24:21Z</dc:date>
    </item>
  </channel>
</rss>

