<?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: Trying to automate macro code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-automate-macro-code/m-p/569181#M160365</link>
    <description>&lt;P&gt;Please describe the general process of what you are actually attempting to do. Not just this bit of code but the overall process.&lt;/P&gt;
&lt;P&gt;Often people will get tied up into attempting one solution that really isn't a good fit but comes close enough for one piece of a problem to keep pounding on it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually including quotes into macro variables is often not a best practice and if you want to compare values with actual date variable&amp;nbsp;then use the actual value is often much cleaner code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Everything you do in those 3 data steps can be done in one with care.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that&lt;/P&gt;
&lt;PRE&gt;%if "&amp;amp;END_DT."="&amp;amp;END_DT." %then %do;&lt;/PRE&gt;
&lt;P&gt;is always true, you are comparing a variable to itself. Which is going to be true. So that particular statement is either incorrect or not needed at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also using a macro parameter with the same name as another macro variable, such as your END_dt, can lead to unexpected results and is likely the cause of that problematic %if above.&lt;/P&gt;
&lt;P&gt;It is also a poor idea to let external macro variables just "fall into" your code. It is better to explicitly pass them than the use you have here:&lt;/P&gt;
&lt;PRE&gt;%macro data_prep(END_DT=);

Proc sql;
create table Tran_&amp;amp;LST_MNTH._DV as
Select cust_id, flg_del from source
where DATE between &amp;amp;LST_SD24. and &amp;amp;LST_ED.; quit;
&lt;/PRE&gt;
&lt;P&gt;where you use 3 macro variables that are not passed to the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you actually &lt;STRONG&gt;need&lt;/STRONG&gt; quotes then the function quote is the way to go. Quote(value) instead of "'"||value||"'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You say&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;It's a huge repetitive code, running for consecutive three months. Only one step is different. Due to only that, code has been written three times. Kindly help me so that i can reduce the length of code.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't see any "huge repetive code". Are you leaving out something? Which step is different&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Jun 2019 17:31:35 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2019-06-26T17:31:35Z</dc:date>
    <item>
      <title>Trying to automate macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-automate-macro-code/m-p/568894#M160239</link>
      <description>&lt;P&gt;Hi Team,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kindly help me to apply %if,%then and %else condition on following case.&lt;/P&gt;
&lt;P&gt;Step 1 : Creating macro variables for dates&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;END_DT = intnx('month',today(),0,'e');&lt;BR /&gt;END_DT_2LST = intnx('month',today(),-1,'e');&lt;BR /&gt;END_DT_CURR = intnx('month',today(),1,'e');&lt;BR /&gt;call symput('END_DT', "'"||put(END_DT,date9.)||"'d");&lt;BR /&gt;call symput('END_DT_2LST', "'"||put(END_DT_2LST,date9.)||"'d");&lt;BR /&gt;call symput('END_DT_CURR', "'"||put(END_DT_CURR,date9.)||"'d");&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data test;&lt;BR /&gt;CURR_ED = intnx('month',&amp;amp;END_DT.,-1,'e');&lt;BR /&gt;CURR_SD1 = intnx('month',&amp;amp;END_DT.,-1,'b');&lt;BR /&gt;LST_ED = intnx('month',&amp;amp;END_DT.,-2,'e');&lt;BR /&gt;LST_SD24 = intnx('month',&amp;amp;END_DT.,-25,'b');&lt;BR /&gt;call symput('CURR_ED', "'"||put(CURR_ED,date9.)||"'d");&lt;BR /&gt;call symput('CURR_SD1', "'"||put(CURR_SD1,date9.)||"'d");&lt;BR /&gt;call symput('LST_ED', "'"||put(LST_ED,date9.)||"'d");&lt;BR /&gt;call symput('LST_SD24', "'"||put(LST_SD24,date9.)||"'d");&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data _null_;&lt;BR /&gt;call symput ("SCR_MNTH", compress(put((intnx('MONTH',&amp;amp;CURR_ED.,1,'e')),monyy5.)));&lt;BR /&gt;call symput ("LST_MNTH",compress(put((intnx('MONTH',&amp;amp;CURR_ED.,0,'e')),monyy5.)));&lt;BR /&gt;call symput ("LST2_MNTH",compress(put((intnx('MONTH',&amp;amp;CURR_ED.,-1,'e')),monyy5.)));&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro data_prep(END_DT=);&lt;/P&gt;
&lt;P&gt;Proc sql;&lt;/P&gt;
&lt;P&gt;create table Tran_&amp;amp;LST_MNTH._DV as&lt;/P&gt;
&lt;P&gt;Select cust_id, flg_del from source&lt;/P&gt;
&lt;P&gt;where&amp;nbsp;DATE between &amp;amp;LST_SD24. and &amp;amp;LST_ED.; quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if "&amp;amp;END_DT."="&amp;amp;END_DT." %then %do;&lt;/P&gt;
&lt;P&gt;Data&amp;nbsp;Tran_&amp;amp;LST_MNTH._DV;&lt;/P&gt;
&lt;P&gt;set&amp;nbsp;Tran_&amp;amp;LST_MNTH._DV;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if "&amp;amp;END_DT."="&amp;amp;END_DT_2LST." %then %do;&lt;/P&gt;
&lt;P&gt;Data&amp;nbsp;Tran_&amp;amp;LST2_MNTH._DV;&lt;/P&gt;
&lt;P&gt;set&amp;nbsp;Tran_&amp;amp;LST_MNTH._DV;&lt;/P&gt;
&lt;P&gt;where flg_del=1;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%if "&amp;amp;END_DT."="&amp;amp;END_DT_CURR." %then %do;&lt;/P&gt;
&lt;P&gt;Data&amp;nbsp;Tran_&amp;amp;SCR_MNTH._DV;&lt;/P&gt;
&lt;P&gt;set&amp;nbsp;Tran_&amp;amp;LST_MNTH._DV;&lt;/P&gt;
&lt;P&gt;where flg_del=0;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%mend data_prep;&lt;/P&gt;
&lt;P&gt;%data_prep(END_DT=&amp;amp;END_DT.);;&lt;/P&gt;
&lt;P&gt;%data_prep(END_DT=&amp;amp;END_DT_2LST.);&lt;/P&gt;
&lt;P&gt;%data_prep(END_DT=&amp;amp;END_DT_CURR.);&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i have just given you the approach, i am trying with it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's a huge repetitive code, running for consecutive three months. Only one step is different. Due to only that, code has been written three times. Kindly help me so that i can reduce the length of code.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jun 2019 18:28:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-automate-macro-code/m-p/568894#M160239</guid>
      <dc:creator>umashankersaini</dc:creator>
      <dc:date>2019-06-25T18:28:17Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to automate macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Trying-to-automate-macro-code/m-p/569181#M160365</link>
      <description>&lt;P&gt;Please describe the general process of what you are actually attempting to do. Not just this bit of code but the overall process.&lt;/P&gt;
&lt;P&gt;Often people will get tied up into attempting one solution that really isn't a good fit but comes close enough for one piece of a problem to keep pounding on it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Actually including quotes into macro variables is often not a best practice and if you want to compare values with actual date variable&amp;nbsp;then use the actual value is often much cleaner code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Everything you do in those 3 data steps can be done in one with care.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that&lt;/P&gt;
&lt;PRE&gt;%if "&amp;amp;END_DT."="&amp;amp;END_DT." %then %do;&lt;/PRE&gt;
&lt;P&gt;is always true, you are comparing a variable to itself. Which is going to be true. So that particular statement is either incorrect or not needed at all.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also using a macro parameter with the same name as another macro variable, such as your END_dt, can lead to unexpected results and is likely the cause of that problematic %if above.&lt;/P&gt;
&lt;P&gt;It is also a poor idea to let external macro variables just "fall into" your code. It is better to explicitly pass them than the use you have here:&lt;/P&gt;
&lt;PRE&gt;%macro data_prep(END_DT=);

Proc sql;
create table Tran_&amp;amp;LST_MNTH._DV as
Select cust_id, flg_del from source
where DATE between &amp;amp;LST_SD24. and &amp;amp;LST_ED.; quit;
&lt;/PRE&gt;
&lt;P&gt;where you use 3 macro variables that are not passed to the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you actually &lt;STRONG&gt;need&lt;/STRONG&gt; quotes then the function quote is the way to go. Quote(value) instead of "'"||value||"'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You say&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;It's a huge repetitive code, running for consecutive three months. Only one step is different. Due to only that, code has been written three times. Kindly help me so that i can reduce the length of code.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't see any "huge repetive code". Are you leaving out something? Which step is different&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jun 2019 17:31:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Trying-to-automate-macro-code/m-p/569181#M160365</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-06-26T17:31:35Z</dc:date>
    </item>
  </channel>
</rss>

