<?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: Macro to create multiple similar files based on different periods in one go in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357439#M83917</link>
    <description>&lt;P&gt;Its not surprising it is slow. &amp;nbsp;You repeatedly go back to the database extracting information. &amp;nbsp;Address your process as a whole;&lt;/P&gt;
&lt;P&gt;1) Extract all the data you will need in one step.&lt;/P&gt;
&lt;P&gt;2) Process the data as you need to.&lt;/P&gt;
&lt;P&gt;I.e.&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table HAVE as select * from DB;
quit;

data want;
  set have;
  /* Assign weekly periods to the data */
  week=...;
run;

/* Create weekly report */
ods excel file=...;
proc report data=want...;
  by week;
  title "Week is #byval1";
  columns _all_;
run;
ods excel close;&lt;/PRE&gt;
&lt;P&gt;There is no need to use macro, nor a need to split the same data into many different blocks - which is also another big resource cost (storage maybe not so much, but read/write on lots of datasets is).&lt;/P&gt;</description>
    <pubDate>Wed, 10 May 2017 12:24:38 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-05-10T12:24:38Z</dc:date>
    <item>
      <title>Macro to create multiple similar files based on different periods in one go</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357428#M83913</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have created this macro, it creates identical tables (same variables etc) for different periods. This code works fine, but it can be time consumig if I wanted to produce it for lots of different periods. Could you please advise me a more elegant way?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you very much&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;/**************************************************/&lt;BR /&gt;/*MACRO TU RUN WEEKLY REPORTS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;BR /&gt;/****************************************************/&lt;/P&gt;
&lt;P&gt;%macro weekly_reports (p, i, insert&amp;nbsp; );&lt;/P&gt;
&lt;P&gt;%put &amp;amp;i.;&lt;BR /&gt;%let days=%sysevalf(%sysfunc(round(365.25/50*&amp;amp;i.,1))-1);&lt;BR /&gt;%put &amp;amp;days.;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;connect to odbc (user=&amp;amp;user. password=&amp;amp;password. dsn=CDM_IQ);&lt;BR /&gt;create table work.Report_&amp;amp;p. as select * from &lt;BR /&gt;connection to odbc(&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;SELECT&lt;/P&gt;
&lt;P&gt;&amp;nbsp; WEEK_END_DATE&lt;/P&gt;
&lt;P&gt;,(CASE WHEN B.CATEGORY ='0' THEN 'CONTROL'&lt;/P&gt;
&lt;P&gt;WHEN B.CATEGORY = '1' THEN 'REDUCED MERCH'&lt;/P&gt;
&lt;P&gt;WHEN B.CATEGORY = '3' THEN 'TARGET'&lt;/P&gt;
&lt;P&gt;WHEN B.CATEGORY IS NULL THEN 'OUTSIDE OF TEST'&lt;/P&gt;
&lt;P&gt;ELSE 'CHECK' END) AS CUST_GROUP&lt;/P&gt;
&lt;P&gt;,(CASE WHEN A.MOST_TRANS &amp;lt;= 1 THEN 'A. ONE OR LESS'&lt;/P&gt;
&lt;P&gt;WHEN A.MOST_TRANS = 2 THEN 'B. TWO'&lt;/P&gt;
&lt;P&gt;WHEN A.MOST_TRANS = 3 THEN 'C. THREE'&lt;/P&gt;
&lt;P&gt;WHEN A.MOST_TRANS = 5 THEN 'D. FIVE'&lt;/P&gt;
&lt;P&gt;WHEN A.MOST_TRANS = 10 THEN 'E. TEN'&lt;/P&gt;
&lt;P&gt;ELSE 'F. OTHER' END) AS MOST_TRANS&lt;/P&gt;
&lt;P&gt;,COUNT(DISTINCT(A.CUSTOMER_ID)) AS CUSTOMERS&lt;/P&gt;
&lt;P&gt;,SUM(A.SALES) AS SALES&lt;/P&gt;
&lt;P&gt;,SUM(A.PLAYS) AS TRANS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;FROM WORK.TABLE_RAW&lt;/P&gt;
&lt;P&gt;WHERE TRANSACTION_DATE BETWEEN &amp;amp;Insert. AND DATEADD(Day, &amp;amp;days., &amp;amp;Insert.)&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;GROUP BY&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;WEEK_END_DATE&lt;/P&gt;
&lt;P&gt;,CUST_GROUP&lt;/P&gt;
&lt;P&gt;,MOST_TRANS ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;);&lt;/P&gt;
&lt;P&gt;disconnect from odbc;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;/*Week by week*/&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%weekly_reports(w1,1,'2016-11-20');&lt;BR /&gt;%weekly_reports(w2,1,'2016-11-27');&lt;BR /&gt;%weekly_reports(w3,1,'2016-12-04');&lt;BR /&gt;%weekly_reports(w4,1,'2016-12-11');&lt;BR /&gt;%weekly_reports(w5,1,'2016-12-18');&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%weekly_reports(w6,1,'2016-12-25');&lt;BR /&gt;%weekly_reports(w7,1,'2017-01-01');&lt;BR /&gt;%weekly_reports(w8,1,'2017-01-08');&lt;BR /&gt;%weekly_reports(w9,1,'2017-01-15');&lt;BR /&gt;%weekly_reports(w10,1,'2017-01-22');&lt;/P&gt;
&lt;P&gt;%weekly_reports(w11,1,'2017-01-29');&lt;BR /&gt;%weekly_reports(w12,1,'2017-02-05');&lt;BR /&gt;%weekly_reports(w13,1,'2017-02-12');&lt;BR /&gt;%weekly_reports(w14,1,'2017-02-19');&lt;BR /&gt;%weekly_reports(w15,1,'2017-02-26');&lt;/P&gt;
&lt;P&gt;%weekly_reports(w16,1,'2017-03-05');&lt;BR /&gt;%weekly_reports(w17,1,'2017-03-12');&lt;BR /&gt;%weekly_reports(w18,1,'2017-03-19');&lt;BR /&gt;%weekly_reports(w19,1,'2017-03-26');&lt;BR /&gt;%weekly_reports(w20,1,'2017-04-02');&lt;/P&gt;
&lt;P&gt;%weekly_reports(w21,1,'2017-04-09');&lt;BR /&gt;%weekly_reports(w22,1,'2017-04-16');&lt;BR /&gt;%weekly_reports(w23,1,'2017-04-23');&lt;BR /&gt;%weekly_reports(w24,1,'2017-04-30');&lt;/P&gt;
&lt;P&gt;/*%weekly_reports(w25,1,'2017-05-07');*/&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;P&gt;&amp;nbsp;&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;P&gt;&amp;nbsp;&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;P&gt;&amp;nbsp;&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;</description>
      <pubDate>Wed, 10 May 2017 11:59:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357428#M83913</guid>
      <dc:creator>Question</dc:creator>
      <dc:date>2017-05-10T11:59:13Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create multiple similar files based on different periods in one go</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357434#M83916</link>
      <description>&lt;P&gt;Can you tell us what part of this code is slow? Is it the PROC SQL? Or something else?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As a side issue, instead of type %weekly_reports(...) 25 times, this could be called in a %do loop inside the macro and eliminate all that typing.&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2017 12:22:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357434#M83916</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-05-10T12:22:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create multiple similar files based on different periods in one go</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357439#M83917</link>
      <description>&lt;P&gt;Its not surprising it is slow. &amp;nbsp;You repeatedly go back to the database extracting information. &amp;nbsp;Address your process as a whole;&lt;/P&gt;
&lt;P&gt;1) Extract all the data you will need in one step.&lt;/P&gt;
&lt;P&gt;2) Process the data as you need to.&lt;/P&gt;
&lt;P&gt;I.e.&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table HAVE as select * from DB;
quit;

data want;
  set have;
  /* Assign weekly periods to the data */
  week=...;
run;

/* Create weekly report */
ods excel file=...;
proc report data=want...;
  by week;
  title "Week is #byval1";
  columns _all_;
run;
ods excel close;&lt;/PRE&gt;
&lt;P&gt;There is no need to use macro, nor a need to split the same data into many different blocks - which is also another big resource cost (storage maybe not so much, but read/write on lots of datasets is).&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2017 12:24:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357439#M83917</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-10T12:24:38Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create multiple similar files based on different periods in one go</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357440#M83918</link>
      <description>Thank you PaigeMiller for your response.&lt;BR /&gt;&lt;BR /&gt;Sorry I didn't express myself properly! This code works fine but copying 25&lt;BR /&gt;time as you said , it's not efficient!&lt;BR /&gt;I was after the do loop..&lt;BR /&gt;&lt;BR /&gt;Thank you&lt;BR /&gt;&lt;BR /&gt;##- Please type your reply above this line. Simple formatting, no&lt;BR /&gt;attachments. -##</description>
      <pubDate>Wed, 10 May 2017 12:25:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357440#M83918</guid>
      <dc:creator>Question</dc:creator>
      <dc:date>2017-05-10T12:25:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to create multiple similar files based on different periods in one go</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357444#M83920</link>
      <description>&lt;P&gt;Try to pull the whole table, starting from your earliest transaction date, into SAS in one sweep. While you do that, you can create your variables cust_group and most_trans. Also create a variable week based on the transaction_date, which you can use as an additional by value when you do the summarization. That will do away with the multiple passes through the ODBC connection.&lt;/P&gt;</description>
      <pubDate>Wed, 10 May 2017 12:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-create-multiple-similar-files-based-on-different/m-p/357444#M83920</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-10T12:33:47Z</dc:date>
    </item>
  </channel>
</rss>

