<?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: adding a rolling month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51836#M10898</link>
    <description>data jan;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=1;&lt;BR /&gt;
run;&lt;BR /&gt;
data feb;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=2;&lt;BR /&gt;
run;&lt;BR /&gt;
data mar;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=3;&lt;BR /&gt;
run;&lt;BR /&gt;
data apr;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=4.4;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
the final output:&lt;BR /&gt;
&lt;BR /&gt;
outlet                     sales1           sales2         sales3            sales4&lt;BR /&gt;
345                           4.4                 3               2                     1</description>
    <pubDate>Mon, 20 Dec 2010 18:33:59 GMT</pubDate>
    <dc:creator>SASPhile</dc:creator>
    <dc:date>2010-12-20T18:33:59Z</dc:date>
    <item>
      <title>adding a rolling month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51832#M10894</link>
      <description>how to get rolling month?&lt;BR /&gt;
24 months of data have to be shown in the dataset.every month we load data,summarize it and create a column "sales". for instance in january it is called sales.&lt;BR /&gt;
in febuary sales is calculated,but we need to bring the sales from january dataset call it sales2 and febuary sales is called sales1.&lt;BR /&gt;
so current month is sales1 and previous month is called sales2 etc etc.&lt;BR /&gt;
The 3rd month,  there'll be Sales1,Sales2 and Sales3, and so on...&lt;BR /&gt;
the data are merged by outlet number.</description>
      <pubDate>Fri, 17 Dec 2010 18:22:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51832#M10894</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2010-12-17T18:22:49Z</dc:date>
    </item>
    <item>
      <title>Re: adding a rolling month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51833#M10895</link>
      <description>If this represents a "reporting requirement", an approach I use is to keep your data "vertical" as observations for each "outlet" and month-period, and use PROC TRANSPOSE at reporting time to generate your "columns" using ID and IDLABEL with PROC TRANSPOSE to create a suitable column heading (or if you prefer, use VAR and generate SAS variables as you have explained).&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.</description>
      <pubDate>Fri, 17 Dec 2010 20:59:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51833#M10895</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-12-17T20:59:32Z</dc:date>
    </item>
    <item>
      <title>Re: adding a rolling month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51834#M10896</link>
      <description>Hello SASPhile,&lt;BR /&gt;
&lt;BR /&gt;
If you need a code please give and an example of input and desired output.&lt;BR /&gt;
&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Fri, 17 Dec 2010 21:31:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51834#M10896</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2010-12-17T21:31:55Z</dc:date>
    </item>
    <item>
      <title>Re: adding a rolling month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51835#M10897</link>
      <description>If I had a choice, like Scott suggested,  I would rather see you change the data structure, but not given the option the macro was fun to write.  See if this is close to the direction that you are going.  The outer %DO loop in the macro would not be needed in production and only appears here because I am stepping through all the months at one time.&lt;BR /&gt;
[pre]data jan;&lt;BR /&gt;
   outlet=345;&lt;BR /&gt;
   sales1=1;&lt;BR /&gt;
   run;&lt;BR /&gt;
data feb;&lt;BR /&gt;
   outlet=345;&lt;BR /&gt;
   sales1=2;&lt;BR /&gt;
   run;&lt;BR /&gt;
data mar;&lt;BR /&gt;
   outlet=345;&lt;BR /&gt;
   sales1=3;&lt;BR /&gt;
   run;&lt;BR /&gt;
data apr;&lt;BR /&gt;
   outlet=345;&lt;BR /&gt;
   sales1=4.4;&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
/* Example target code to be generated by the macro SALESMO*/&lt;BR /&gt;
/*data totjan;*/&lt;BR /&gt;
/*   set jan;*/&lt;BR /&gt;
/*   run;*/&lt;BR /&gt;
/*data totfeb;*/&lt;BR /&gt;
/*   merge totjan(rename=(sales1=sales2))*/&lt;BR /&gt;
/*      feb;*/&lt;BR /&gt;
/*   by outlet;*/&lt;BR /&gt;
/*   run;*/&lt;BR /&gt;
/*data totmar;*/&lt;BR /&gt;
/*   merge totfeb(rename=(sales2=sales3 sales1=sales2))*/&lt;BR /&gt;
/*         mar;*/&lt;BR /&gt;
/*   by outlet;*/&lt;BR /&gt;
/*   run;*/&lt;BR /&gt;
/*proc print data=totmar;*/&lt;BR /&gt;
   run;&lt;BR /&gt;
&lt;BR /&gt;
%macro salesmo(mo=jan);&lt;BR /&gt;
%local monum molast i m;&lt;BR /&gt;
%let monum = %sysfunc(month(%sysfunc(inputn(&amp;amp;mo.2010,monyy7.))));&lt;BR /&gt;
%*put monum &amp;amp;monum;&lt;BR /&gt;
%do I = 1 %to &amp;amp;monum;&lt;BR /&gt;
   data tot&amp;amp;mo;&lt;BR /&gt;
&lt;BR /&gt;
   %if i=1 %then %do;&lt;BR /&gt;
      %* The month is january;&lt;BR /&gt;
      set &amp;amp;mo;&lt;BR /&gt;
   %end;&lt;BR /&gt;
   %else %do;&lt;BR /&gt;
      %* Determine the name of the previous month;&lt;BR /&gt;
      %let molast = %sysfunc(putn(%sysfunc(intnx(month,%sysfunc(mdy(&amp;amp;i,1,2010)),-1)),monname3.));&lt;BR /&gt;
      merge tot&amp;amp;molast(rename=(&lt;BR /&gt;
            %do m=&amp;amp;i %to 2 %by -1;&lt;BR /&gt;
               %* build the rename pairs in descending order;&lt;BR /&gt;
               sales%eval(&amp;amp;m-1)=sales&amp;amp;m&lt;BR /&gt;
            %end;&lt;BR /&gt;
                           ))&lt;BR /&gt;
            %* Read the current months data;&lt;BR /&gt;
            &amp;amp;mo;&lt;BR /&gt;
      by outlet;&lt;BR /&gt;
   %end;&lt;BR /&gt;
   run;&lt;BR /&gt;
%end;&lt;BR /&gt;
%mend salesmo;&lt;BR /&gt;
%salesmo(mo=apr)&lt;BR /&gt;
proc print data=totapr;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
For renaming the variables, which is really the heart of your question, this solution takes advantage of the RENAME= options execution properties.&lt;BR /&gt;
[pre] merge totfeb(rename=(sales2=sales3 sales1=sales2))[/pre]&lt;BR /&gt;
here sales2 is renamed to sales3 before sales1 is renamed to sales2</description>
      <pubDate>Fri, 17 Dec 2010 21:50:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51835#M10897</guid>
      <dc:creator>ArtC</dc:creator>
      <dc:date>2010-12-17T21:50:43Z</dc:date>
    </item>
    <item>
      <title>Re: adding a rolling month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51836#M10898</link>
      <description>data jan;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=1;&lt;BR /&gt;
run;&lt;BR /&gt;
data feb;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=2;&lt;BR /&gt;
run;&lt;BR /&gt;
data mar;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=3;&lt;BR /&gt;
run;&lt;BR /&gt;
data apr;&lt;BR /&gt;
outlet=345;&lt;BR /&gt;
sales1=4.4;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
the final output:&lt;BR /&gt;
&lt;BR /&gt;
outlet                     sales1           sales2         sales3            sales4&lt;BR /&gt;
345                           4.4                 3               2                     1</description>
      <pubDate>Mon, 20 Dec 2010 18:33:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51836#M10898</guid>
      <dc:creator>SASPhile</dc:creator>
      <dc:date>2010-12-20T18:33:59Z</dc:date>
    </item>
    <item>
      <title>Re: adding a rolling month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51837#M10899</link>
      <description>Did you look at PROC TRANSPOSE?&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;BR /&gt;
&lt;BR /&gt;
Suggested Google advanced search argument, this topic / post:&lt;BR /&gt;
&lt;BR /&gt;
proc transpose site:sas.com</description>
      <pubDate>Mon, 20 Dec 2010 18:36:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51837#M10899</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2010-12-20T18:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: adding a rolling month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51838#M10900</link>
      <description>Hello SASPhile,&lt;BR /&gt;
&lt;BR /&gt;
I think this is a major part of the implementation that Sbb suggests:&lt;BR /&gt;
[pre]&lt;BR /&gt;
data i;&lt;BR /&gt;
  set jan(in=j) feb(in=f) mar(in=m) apr(in=a);&lt;BR /&gt;
  if j then mt=1;&lt;BR /&gt;
  if f then mt=2;&lt;BR /&gt;
  if m then mt=3;&lt;BR /&gt;
  if a then mt=4;&lt;BR /&gt;
run;&lt;BR /&gt;
proc transpose data=i out=r(drop=_name_) prefix=sales;&lt;BR /&gt;
  id mt;&lt;BR /&gt;
  var sales1;&lt;BR /&gt;
  by outlet;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
Sincerely,&lt;BR /&gt;
SPR</description>
      <pubDate>Mon, 20 Dec 2010 21:10:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/adding-a-rolling-month/m-p/51838#M10900</guid>
      <dc:creator>SPR</dc:creator>
      <dc:date>2010-12-20T21:10:58Z</dc:date>
    </item>
  </channel>
</rss>

