<?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: SAS: Rotating a dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373048#M276172</link>
    <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the responses, it is now kind of working, as per below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
	create table agg_transactions as
		select Address, 'Mapped Account'n, Period, Period_Name, Sum(Amount) as amount
		from tf.transactions_&amp;amp;fund
		group by Address, 'Mapped Account'n, Period, Period_Name;
quit;
proc transpose data=agg_transactions out = work.cashflows (drop = _NAME_);
	by Address 'Mapped Account'n;
 	var Amount; 
	id Period_Name;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I've done is create a new variable "Period_Name" as a character with format "Date." (e.g. 31Jan17) and I've then tried to use this as my grouping variable and my ID, which seems to work, and I think gets around the problem of using dates as&amp;nbsp;variable names?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Jul 2017 16:32:25 GMT</pubDate>
    <dc:creator>MikeFranz</dc:creator>
    <dc:date>2017-07-04T16:32:25Z</dc:date>
    <item>
      <title>SAS: Rotating a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373026#M276168</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have&amp;nbsp;seen a couple of posts on this, but I still can't get my code to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset which has the following layout:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Address Account     Period      Amount
Prop 1  Rent        31/01/2017  1500
Prop1   Rent        28/02/2017  1500
Prop1   Insurance   31/01/2017  -400
Prop1   Insurance   31/02/2017  -50
Prop1   Insurance   28/01/2017  -350
Prop1   PM Fees     31/01/2017  -100
Prop2   Rent        31/01/2017  600
Prop2   Rent        31/01/2017  600
Prop2   Rent        28/02/2017  1200&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking to rotate this dataset such that the output is as below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;Address Account    31/01/2017 28/02/2017
Prop1    Rent      1500        1500
Prop1   Insurance  -400        -350
Prop1   PM Fees    -100           0
Prop2   Rent       1200        1200&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;etc. i.e. I would like to aggregate up across Address and Account, and then rotate the Period column into variables, such that there is only one row for each Address/Account pair.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Could anyone help me with this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Mike&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2017 14:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373026#M276168</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-07-04T14:50:50Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Rotating a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373031#M276169</link>
      <description>&lt;P&gt;The task is called transposing going from logn to wide.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For your specific taks you can do it in a number of ways, proc summary for instance, arrays etc. &amp;nbsp;I will show doing the two steps separately:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table INTER as
  select  ADDRESS, 
          ACCOUNT,
          PERIOD,
          sum(AMOUNT) as AMOUNT
  from    HAVE
  group by ADDRESS,ACCOUNT,PERIOD;
quit;

proc transpose data=inter out=want;
  by address account;
  var amount;
  idlabel period;
run;&lt;/PRE&gt;
&lt;P&gt;Note that the above will only label the variables with the date, its really not a good idea to name the variables per the date as it makes your code 10x more difficult then.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2017 15:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373031#M276169</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-04T15:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Rotating a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373034#M276170</link>
      <description>&lt;P&gt;Similar to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;'s response, but using proc summary and proc transpose. Regardless, with using either method, I think you want to use id rather than idlabel.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, your data had some errors in it like a date of Feb 31 and an extra space in one of the addresses:&lt;/P&gt;
&lt;PRE&gt;data have;
  informat Account $10.;
  informat Period anydtdte10.;
  format Period ddmmyy10.;
  input Address $ Account &amp;amp;    Period      Amount;
  cards;
Prop1  Rent        31/01/2017  1500
Prop1   Rent        28/02/2017  1500
Prop1   Insurance   31/01/2017  -400
Prop1   Insurance   31/01/2017  -50
Prop1   Insurance   28/01/2017  -350
Prop1   PM Fees     31/01/2017  -100
Prop2   Rent        31/01/2017  600
Prop2   Rent        31/01/2017  600
Prop2   Rent        28/02/2017  1200
;

proc summary data=have nway;
  class Address Account Period;
  var amount;
  output out=need (drop=_:) sum=;
run;

proc transpose data=need out=want;
  by Address Account;
  var Amount;
  id Period;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2017 15:15:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373034#M276170</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-04T15:15:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Rotating a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373036#M276171</link>
      <description>&lt;P&gt;Hi &amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13711"&gt;@art297&lt;/a&gt;, I was particularly avoiding the use of id as having columns named:&lt;/P&gt;
&lt;P&gt;_31_01_201 etc.&lt;/P&gt;
&lt;P&gt;Are really difficult to program with. &amp;nbsp;Fine with putting the date in the label - although it becomes useless then - but definately not as variable names. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2017 15:28:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373036#M276171</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-04T15:28:52Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Rotating a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373048#M276172</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the responses, it is now kind of working, as per below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;
	create table agg_transactions as
		select Address, 'Mapped Account'n, Period, Period_Name, Sum(Amount) as amount
		from tf.transactions_&amp;amp;fund
		group by Address, 'Mapped Account'n, Period, Period_Name;
quit;
proc transpose data=agg_transactions out = work.cashflows (drop = _NAME_);
	by Address 'Mapped Account'n;
 	var Amount; 
	id Period_Name;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I've done is create a new variable "Period_Name" as a character with format "Date." (e.g. 31Jan17) and I've then tried to use this as my grouping variable and my ID, which seems to work, and I think gets around the problem of using dates as&amp;nbsp;variable names?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks for your help!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2017 16:32:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373048#M276172</guid>
      <dc:creator>MikeFranz</dc:creator>
      <dc:date>2017-07-04T16:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: SAS: Rotating a dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373067#M276173</link>
      <description>&lt;P&gt;Not really. &amp;nbsp;The reason is this:&lt;/P&gt;
&lt;P&gt;Labels - these are free text used for display purposes and have no other function so can contain anything.&lt;/P&gt;
&lt;P&gt;Names - these are used only in the programming. &amp;nbsp;For logical operations these should be simple and consistent to ease programming activity. &amp;nbsp;&lt;/P&gt;
&lt;P&gt;Take an example, you need to alter each of the variables in your transposed dataset, now if you know where the first is and the last you can simplfy to:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array vals{*} _31_02_2017--_12_01_2019;
  do i=1 to dim(vals);
    ...;
  end;&lt;BR /&gt;  final_total=sum(_31_02_2017--_12_01_2019);
run;&lt;/PRE&gt;
&lt;P&gt;However if you have a consistent naming convention (e.g. col1 col2 etc.) you can simplify quite a lot:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  array vals{*} col:;
  do i=1 to dim(vals);
    ...;
  end;
  final_total=sum(of col:);
run;
&lt;/PRE&gt;
&lt;P&gt;Just in this simple example you can see how consistent naming simplfies the code and simple code leads to easy to read and maintain code.&lt;/P&gt;
&lt;P&gt;Second to this, your putting "data" into a place where it is hard to process, say you want to manipulate date, move it one day forward, how would you go about doing that, maybe a mass of macro code looping over metadata? &amp;nbsp;Whereas keep the date in the data, makes processing simpler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Jul 2017 17:54:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Rotating-a-dataset/m-p/373067#M276173</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-07-04T17:54:49Z</dc:date>
    </item>
  </channel>
</rss>

