<?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 can I tranform a long table to wide table? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341865#M78315</link>
    <description>&lt;P&gt;When someone mentions 'pivot table' in SAS i always think of PROC TABULATE.&amp;nbsp; The following code should get close to what you want.&amp;nbsp; The multilabel format allows tabulate to combine the CLASS levels and the NOTSORTED along with the PRELOADFMT preserves the format order.&amp;nbsp; For simplicity I took the text date and built a SAS date so that a data format could be applied.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   value $txncode (multilabel notsorted)
   'F','L' = 'FL'
   'F' = 'F'
   'L' = 'L';
   run;

data Txn(keep=date txnamount txntype);    
   input Year Month $ TxnAmount TxnType $;
   dtstr = cats('01',month,year);
   date = input(dtstr,date9.); 
   datalines; 
2017 Jan 100 F
2017 Jan 100 L
2017 Feb 100 F
2017 Feb 200 L
2017 Feb 300 F
2017 Feb 400 L
run; 
proc tabulate data=txn;
   class date / order=internal;
   class txntype/mlf preloadfmt order=data;
   var txnamount;
   table txntype=' ',
         date=' '*txnamount=' '*sum=' '/
         box='Type';
   format txntype $txncode.
          date    monyy7.;
   run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 17 Mar 2017 05:36:31 GMT</pubDate>
    <dc:creator>ArtC</dc:creator>
    <dc:date>2017-03-17T05:36:31Z</dc:date>
    <item>
      <title>How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341850#M78307</link>
      <description>&lt;PRE&gt;data Txn;    
   input Year Month $ TxnAmount TxnType $;    
   datalines; 
2017 Jan 100 F
2017 Jan 100 L
2017 Feb 100 F
2017 Feb 200 L
2017 Feb 300 F
2017 Feb 400 L
; 

/*I want this*/ 
data TxnReport;    
   input TxnType $ Jan2017 Feb2017 ;    
   datalines; 
FL 200 1000 *FL is F and L combined; &lt;BR /&gt;F 100 400
L 100 600
; &lt;/PRE&gt;&lt;P&gt;Currently, I'm using a mix of PROC SQL to get the summation and then PROC TRANPOSE. And finally a DATA Step to append the tables. Does SAS has a proc for pivot table to get this result in lesser and more readable code?&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 03:46:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341850#M78307</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-03-17T03:46:55Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341865#M78315</link>
      <description>&lt;P&gt;When someone mentions 'pivot table' in SAS i always think of PROC TABULATE.&amp;nbsp; The following code should get close to what you want.&amp;nbsp; The multilabel format allows tabulate to combine the CLASS levels and the NOTSORTED along with the PRELOADFMT preserves the format order.&amp;nbsp; For simplicity I took the text date and built a SAS date so that a data format could be applied.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
   value $txncode (multilabel notsorted)
   'F','L' = 'FL'
   'F' = 'F'
   'L' = 'L';
   run;

data Txn(keep=date txnamount txntype);    
   input Year Month $ TxnAmount TxnType $;
   dtstr = cats('01',month,year);
   date = input(dtstr,date9.); 
   datalines; 
2017 Jan 100 F
2017 Jan 100 L
2017 Feb 100 F
2017 Feb 200 L
2017 Feb 300 F
2017 Feb 400 L
run; 
proc tabulate data=txn;
   class date / order=internal;
   class txntype/mlf preloadfmt order=data;
   var txnamount;
   table txntype=' ',
         date=' '*txnamount=' '*sum=' '/
         box='Type';
   format txntype $txncode.
          date    monyy7.;
   run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2017 05:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341865#M78315</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-03-17T05:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341878#M78322</link>
      <description>Hi ArtC, thank you for this. It helps to know that there's a more simpler way. Is it possible to get the output of the proc tabulate as it is in a sas dataset format?&lt;BR /&gt;&lt;BR /&gt;When I add the out= option, the table has a different structure though.</description>
      <pubDate>Fri, 17 Mar 2017 07:30:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341878#M78322</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-03-17T07:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341903#M78332</link>
      <description>&lt;P&gt;Instead of proc tabulate use:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc summary data=work.txn nway;   
   class TxnType / mlf preloadfmt order=data;
   class date;
   var TxnAmount;
   format txntype $txncode.;
   output out=work.summed(drop=_:) sum=;
run;



proc transpose data=work.summed out=work.want(drop=_name_);
   by TxnType notsorted;
   var TxnAmount;
   id date;
   format date monyy7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Mar 2017 09:42:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341903#M78332</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2017-03-17T09:42:36Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341911#M78335</link>
      <description>This is pretty much the solution I'm currently using though I didn't use proc format as to avoid using options.&lt;BR /&gt;But thanks!</description>
      <pubDate>Fri, 17 Mar 2017 10:11:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341911#M78335</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-03-17T10:11:16Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341914#M78336</link>
      <description>I think I have the answer. With proc tabulate, I'm not able to get the output in sas dataset format as it's not a sas table. Is it possible to export the report into Excel?&lt;BR /&gt;The table must be as it is.</description>
      <pubDate>Fri, 17 Mar 2017 10:14:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/341914#M78336</guid>
      <dc:creator>afiqcjohari</dc:creator>
      <dc:date>2017-03-17T10:14:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342012#M78365</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;Hi ArtC, thank you for this. It helps to know that there's a more simpler way. Is it possible to get the output of the proc tabulate as it is in a sas dataset format?&lt;BR /&gt;&lt;BR /&gt;When I add the out= option, the table has a different structure though.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The structure of the OUT= dataset from proc tabulate has to handle nested data in both rows and columns, stacked data and the result of multiple tables from a single procedure call. So there is much information that you likely don't care about. But if your really need a data set&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc transpose data=work.txnout out=work.txnreport (drop=_name_);
  by txntype notsorted ;
  id date;
  var TxnAmount_sum;
run;&lt;/PRE&gt;
&lt;P&gt;where work.txnout would be the name of the OUT= option on tabulate&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ods Excel file="Your excelfile&amp;nbsp;name goes here";&lt;/P&gt;
&lt;P&gt;&amp;lt;proc tabulate&amp;nbsp; code goes here&amp;gt;&lt;/P&gt;
&lt;P&gt;Ods Excel close;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or Ods tagsets.excelxp instead of Excel if on an earlier version of SAS.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 15:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342012#M78365</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-17T15:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342040#M78372</link>
      <description>&lt;P&gt;if the data set is the objective then i would agree that PROC SUMMARY is better than TABULATE.&amp;nbsp; You can achieve the same levels of summarization either way, but SUMMARY is designed to build the data set while TABULATE is designed to create the table.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 16:33:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342040#M78372</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-03-17T16:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342183#M78432</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; wrote:&lt;BR /&gt;I think I have the answer. With proc tabulate, I'm not able to get the output in sas dataset format as it's not a sas table. Is it possible to export the report into Excel?&lt;BR /&gt;The table must be as it is.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The output using the OUT= option on Proc Tabulate &lt;STRONG&gt;is&lt;/STRONG&gt; a SAS dataset.&lt;/P&gt;
&lt;P&gt;It just is in a very specific form for a very specific purpose. I have several processes where I post process data from proc tabulate as it is very much easier than the multiple data step or summary passes and convoluted logic to line things up.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Mar 2017 22:38:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342183#M78432</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-03-17T22:38:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I tranform a long table to wide table?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342220#M78449</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/130031"&gt;@afiqcjohari&lt;/a&gt; Since we are providing various alternatives another very flexible way to summarize and tranform in the same step is with PROC REPORT.&amp;nbsp; Although i believe used less often to create a data set, it also has an OUT= option.&amp;nbsp; In each of the three methods that have been presented to you the MLF option is an alternative way to summarize across CLASS levels.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc report data=txn out=wide;
   column txntype date,txnamount;
   define txntype / group f=$txncode. mlf preloadfmt order=data;
   define date / across f=monyy. order=internal;
   define txnamount / sum;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 18 Mar 2017 04:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-tranform-a-long-table-to-wide-table/m-p/342220#M78449</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2017-03-18T04:49:54Z</dc:date>
    </item>
  </channel>
</rss>

