<?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: HELP WITH MACRO FUNCTION in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358620#M84299</link>
    <description>It works. Thank you so much !!</description>
    <pubDate>Sun, 14 May 2017 22:18:11 GMT</pubDate>
    <dc:creator>tuananhle269xx</dc:creator>
    <dc:date>2017-05-14T22:18:11Z</dc:date>
    <item>
      <title>HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358167#M84139</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I try to create the function TABLE_CREATION(YYYYMM)&lt;/P&gt;&lt;P&gt;It means for each month, it will create one table from given dataset&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%MACRO TABLE_CREATION(YYYYMM);&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;create table info (keep = id datadate compname sales prm fyearq) as&lt;BR /&gt;select a.*&lt;BR /&gt;from statistic a&lt;BR /&gt;where id in ('011636', '014489')&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;and intnx(month,"&amp;amp;YYYYMM"d,-3) &amp;lt;= datadate &amp;lt;=&amp;nbsp;&lt;SPAN&gt;"&amp;amp;YYYYMM"d;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;%MEND GET_DATA;&lt;/P&gt;&lt;P&gt;%GET_DATA(201612);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The red line got something when calling macro variables. Does anyone know the problem? Thank you &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 May 2017 07:54:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358167#M84139</guid>
      <dc:creator>tuananhle269xx</dc:creator>
      <dc:date>2017-05-12T07:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358175#M84141</link>
      <description>&lt;P&gt;There are quite a few topics on here which match your question, please try search function in future. &amp;nbsp;Date literals, which are indicated by a trailing d, can only be DDMMMYYYY format which is why its not working. &amp;nbsp;See below fixed code. &amp;nbsp;Also, why is this a macro, there is no need for it? &amp;nbsp;See second example. &amp;nbsp;Just to note, I removed the keep and replace the variables in the select - if you want to use SQL best to use SQL syntax and avoid putting datastep things in it otherwise the SQL is not portable. &amp;nbsp;Out of interest why not just use base SAS per example 3?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example 1 - fixing the code given - note there were quite a few issues (macro not called the same is one):&lt;/P&gt;
&lt;PRE&gt;%macro table_creation (d=); 
  proc sql;
    create table INFO as
    select  A.ID,
            A.DATADATE,
            A.COMPNAME,
            A.SALES,
            A.PRM,
            A.FYEARQ
    from    STATISTIC A
    where   ID in ('011636', '014489')
      and   intnx(month,"&amp;amp;d."d,-3) &amp;lt;= datadate &amp;lt;= "&amp;amp;d."d;&lt;BR /&gt;  quit;
%mend table_creation;
%table_creation (01JAN2015);&lt;/PRE&gt;
&lt;P&gt;Example 2: No macro, just a %let statement to change:&lt;/P&gt;
&lt;PRE&gt;%let d=01JAN2015;

proc sql;
  create table INFO as
  select  A.ID,
          A.DATADATE,
          A.COMPNAME,
          A.SALES,
          A.PRM,
          A.FYEARQ
  from    STATISTIC A
  where   ID in ('011636', '014489')
    and   intnx(month,"&amp;amp;d."d,-3) &amp;lt;= datadate &amp;lt;= "&amp;amp;d."d;
quit;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example 3 - simple datastep:&lt;/P&gt;
&lt;PRE&gt;%let d=01JAN2015;

data want;
  set statistic (keep=id datadate compname sales prm fyearq);
  where id in ('011636', '014489') and intnx(month,"&amp;amp;d."d,-3) &amp;lt;= datadate &amp;lt;= "&amp;amp;d."d;
run;&lt;/PRE&gt;</description>
      <pubDate>Fri, 12 May 2017 08:24:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358175#M84141</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-12T08:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358181#M84144</link>
      <description>&lt;P&gt;Learn to walk before you try to run, and only when you're an accomplished runner spread your wings and try to fly&lt;/P&gt;
&lt;P&gt;(SAS concepts like dates - walk&lt;/P&gt;
&lt;P&gt;SQL and advanced programming - run&lt;/P&gt;
&lt;P&gt;macro and further advancement - fly)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 May 2017 08:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358181#M84144</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-12T08:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358231#M84156</link>
      <description>&lt;P&gt;The idea looks good, but might need a little tweaking to get the right date range. &amp;nbsp;If you were to run the program using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let d=01JAN2015;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Which range of dates should be selected? &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 12 May 2017 13:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358231#M84156</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-05-12T13:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358402#M84207</link>
      <description>&lt;P&gt;Thank you so much for your reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I try to use the first code but it didn't work. SAS notifies error:&amp;nbsp;More positional parameters found than defined.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;libname myLib "C:\";&lt;BR /&gt;%macro table_creation (d=);&lt;BR /&gt;  rsubmit;&lt;BR /&gt;  libname comp '/sasdata/file'; &amp;nbsp;
  proc sql;
    create table INFO as
    select  A.ID,
            A.DATADATE,
            A.COMPNAME,
            A.SALES,
            A.PRM,
            A.FYEARQ
    from    STATISTIC A
    where   ID in ('011636', '014489')
      and   intnx(month,"&amp;amp;d."d,-3) &amp;lt;= datadate &amp;lt;= "&amp;amp;d."d;&lt;BR /&gt;  quit;&lt;BR /&gt;&lt;BR /&gt;proc download data= INFO out=myLib.example;&lt;BR /&gt;run;&lt;BR /&gt;endrsubmit;&lt;BR /&gt;&lt;BR /&gt;proc export&lt;BR /&gt;DATA=myLib.example&lt;BR /&gt;OUTFILE="C:\&amp;amp;d.dta"&lt;BR /&gt;DBMS=dta REPLACE;&lt;BR /&gt;run;&lt;BR /&gt;
%mend table_creation;
%table_creation (31DEC2016);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have another small question if you don't mind. How can I create the loop to create table for each month over a particular time period (let's say 31DEC1970 to 31DEC2016) and export to the .dta file monthly (name that month and year) with SAS Code above ?&lt;/P&gt;&lt;P&gt;I really appreciate that. &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 13 May 2017 01:27:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358402#M84207</guid>
      <dc:creator>tuananhle269xx</dc:creator>
      <dc:date>2017-05-13T01:27:46Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358416#M84213</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/143584"&gt;@tuananhle269xx&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;It's may be better if you don't pack too much into a macro. Especially try and avoid to have any RSUBMIT blocks intermingled with macro definitions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here a code sampel for you to build upon it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* set start and end date */
%let startdt=01Oct1949;
%let enddt	=01Sep1950;

/* align dates to month begin and end date */
%let startdt=%sysfunc(intnx(month,"&amp;amp;startdt"d,0,b),date9.);
%let enddt=%sysfunc(intnx(month,"&amp;amp;enddt"d,0,e),date9.);

/* upload macro variables to remote session */
%syslput _user_;

/* download the data from remote to local session */
/*rsubmit test wait=yes ;*/
rsubmit;
	proc download 
		data=sashelp.airline (keep=date air region) 
		out=work.airline;
		where region="ALL" 
			and date between intnx('month',"&amp;amp;startdt"d,0,'b') and intnx('month',"&amp;amp;enddt"d,0,'e')
	; 
	run;
endrsubmit;

/* create monthly external files */
%macro spitItOut();
  %local month_cnt month_list;
  %let month_cnt=0;
  proc sql noprint;
    select count(*), put(month_begin,date9.)
        into :month_cnt, :month_list separated by ' '
    from
      (
        select 
          distinct (intnx('month',date,0,'b')) as month_begin 
        from work.airline
      )
    ;
  quit;

  %put &amp;amp;=month_list;
  %put &amp;amp;=month_cnt;

  %do i=1 %to &amp;amp;month_cnt;
    %local thisMonth thisMonthFormated;
    %let thisMonth=%scan(&amp;amp;month_list,&amp;amp;i);

    proc export
      data=work.airline(where=(intnx('month',date,0,'b')="&amp;amp;thisMonth"d))
      outfile="c:\temp\out_&amp;amp;thisMonth..dta"
      dbms=dta replace;
    run;
  %end;
%mend;
%spitItOut()&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 13 May 2017 04:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358416#M84213</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-05-13T04:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358560#M84291</link>
      <description>&lt;P&gt;Several things, lets start by your issue:&lt;/P&gt;
&lt;PRE&gt;%table_creation (d=31DEC2016);&lt;/PRE&gt;
&lt;P&gt;Note that I now put d= before it. &amp;nbsp;There is two methods of parameters to macros, positional and named. &amp;nbsp; Take:&lt;BR /&gt;%macro test (a,b);&lt;/P&gt;
&lt;P&gt;This is positional, a will always have the first value, b the second, this:&lt;/P&gt;
&lt;P&gt;%macro test (a=,b=);&lt;/P&gt;
&lt;P&gt;Now in this it doesn't matter if I put a= second or at any point, a will always be what is named. &amp;nbsp;I would advise to always use named parameters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now with your second point, firstly I would say that creating monthly files is not a great idea, it just means lots of files to import. &amp;nbsp;Either have cumulative or incremental files, i.e. all data each tranfer, or all data from a point each transfer both in one file. &amp;nbsp;Simple to send and simple to import. &amp;nbsp;I see this wanting to put dates into filenames a lot and all the code assiciated with it is messy and prone to failure, not to mention it doesn't add any value. &amp;nbsp;One data file with a date/time field is both easier to use and contains the same information. &amp;nbsp;If I had to do it this way, and wouldn't recommend I would do:&lt;/P&gt;
&lt;PRE&gt;data _null_;
  do months=0 to 12;
    call execute('proc export data=mylib.example 
                        outfile="c:\'||strip(put(intnx('month',"&amp;amp;d."d,i),date9.))||'.dta"
                        dbms=dta replace;
                        where date_in_date between "&amp;amp;d."d and '||intnx('month',"&amp;amp;d.",i)||';
                      run;');
  end;
run;&lt;/PRE&gt;
&lt;P&gt;This will generate a proc export code for each month between &amp;amp;d and &amp;amp;d + 12.&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2017 14:58:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358560#M84291</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-14T14:58:42Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358597#M84295</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;Sorry for this inconvenience. I still run into one trouble. Thank you for your help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;libname myLib "C:\";&lt;BR /&gt;%macro table_creation (d=);&lt;BR /&gt;  rsubmit;&lt;BR /&gt;  libname comp '/sasdata/file'; &amp;nbsp;
  proc sql;
    create table INFO as
    select  A.ID,
            A.DATADATE,
            A.COMPNAME,
            A.SALES,
            A.PRM,
            A.FYEARQ
    from    STATISTIC A
    where   ID in ('011636', '014489')
      and   &lt;FONT color="#FF0000"&gt;intnx(month,"&amp;amp;d."d,-3) &amp;lt;= datadate &amp;lt;= "&amp;amp;d."d;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;/**  WARNING: Apparent symbolic reference D not resolved.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;ERROR: Invalid date/time/datetime constant "&amp;amp;d."d.  **/&lt;/FONT&gt;&lt;BR /&gt;  quit;&lt;BR /&gt;&lt;BR /&gt;proc download data= INFO out=myLib.example;&lt;BR /&gt;run;&lt;BR /&gt;endrsubmit;&lt;BR /&gt;
%mend table_creation;
%table_creation (d=31DEC2016);&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2017 20:03:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358597#M84295</guid>
      <dc:creator>tuananhle269xx</dc:creator>
      <dc:date>2017-05-14T20:03:10Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358599#M84296</link>
      <description>&lt;P&gt;If you want to reference a macro variable in code submitted to a remote SAS session then make sure the macro variable is defined in that session.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro table_creation (d=);
  %syslput d=&amp;amp;d;
  rsubmit;
...
 intnx(month,"&amp;amp;d."d,-3) &amp;lt;= datadate &amp;lt;= "&amp;amp;d."d 
...
endrsubmit;
%mend table_creation;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 14 May 2017 20:10:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358599#M84296</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-05-14T20:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358604#M84297</link>
      <description>&lt;P&gt;Macro variable d is defined in the local macrovar table in your local SAS session. The code in the rsubmit runs in the remote environment that has no access to the macro variable table of your local session.&lt;/P&gt;
&lt;P&gt;Look if the %syslput macro statement helps in your situation.&lt;/P&gt;</description>
      <pubDate>Sun, 14 May 2017 21:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358604#M84297</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-05-14T21:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358620#M84299</link>
      <description>It works. Thank you so much !!</description>
      <pubDate>Sun, 14 May 2017 22:18:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358620#M84299</guid>
      <dc:creator>tuananhle269xx</dc:creator>
      <dc:date>2017-05-14T22:18:11Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358621#M84300</link>
      <description>Thank you. It works &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 14 May 2017 22:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358621#M84300</guid>
      <dc:creator>tuananhle269xx</dc:creator>
      <dc:date>2017-05-14T22:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: HELP WITH MACRO FUNCTION</title>
      <link>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358703#M84311</link>
      <description>&lt;P&gt;Well, month should have quotes around it, that might be it, this works fine however, and I can't tell from what you have posted:&lt;/P&gt;
&lt;PRE&gt;data temp;
  set sashelp.class;
  datadate='09JAN2017'd;
  format datadate date9.;
run;
%macro table_creation (d=);
  proc sql ;
    create table INFO as
    select  *
    from    TEMP
    where   SEX in ('M')
      and   intnx('month',"&amp;amp;d."d,-3) &amp;lt;= datadate &amp;lt;= "&amp;amp;d."d;
  quit;
%mend table_creation;
%table_creation (d=31DEC2016);&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 May 2017 11:34:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/HELP-WITH-MACRO-FUNCTION/m-p/358703#M84311</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-05-15T11:34:12Z</dc:date>
    </item>
  </channel>
</rss>

