<?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 if then do question in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634119#M188201</link>
    <description>&lt;P&gt;I'm not going to re-write your entire macro code, but let me give you some general principles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro variables should not be formatted (except for display in titles or labels or report). So&lt;/P&gt;
&lt;PRE&gt;%let Today_SAS  = %sysfunc(date(),Date9.);&lt;/PRE&gt;
&lt;P&gt;really ought to be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Today_SAS  = %sysfunc(date());&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for any logical or arithmetical use, which is what you appear to be doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, macro variable values almost never need quotes around them, or parentheses around them. You spend a lot of time formatting and unformatting and adding quotes and then removing them. This is all unnecessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The exception is when you need to hard-code a specific date, such as you have 01MAR2020. Then you can use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; %if &amp;amp;Quarter_SAS = %sysevalf('01MAR2020'd) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Really, start from scratch, re-write your code without formatting and without quotes and parentheses, following the above principles, and it will work a lot better with a lot less effort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 23 Mar 2020 14:15:06 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2020-03-23T14:15:06Z</dc:date>
    <item>
      <title>Macro if then do question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634114#M188198</link>
      <description>&lt;P&gt;So macro programming has always been a weakness for me, but I am trying to do something fairly simple to give it a go for date automation.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So what I want to happen is based on the run date, it will create the Quartercal for me based on my logic below.&amp;nbsp; So I am missing something when running this macro but I am not sure what it is.&amp;nbsp; I haven't done many of these if, then, do, else if, types before.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro Quarter_SAS(Quartercal);
%let Today_SAS  = %sysfunc(date(),Date9.);
%LET Run_Date   = %SYSFUNC(INPUTN(&amp;amp;Today_SAS., Date9.));
%LET Quarter_SAS  = %SYSFUNC(INTNX(MONTH, &amp;amp;Run_Date., 0., B), Date9.);
   %if &amp;amp;Quarter_SAS = ('01MAR2020') %then
      %do;
      %let Quartercal=('Q12020');
      %end;
   %else %if &amp;amp;Quarter_SAS = ('01AUG2020') %then
      %do;
      %let Quartercal=('Q22020');
   %end;
   %else %if &amp;amp;Quarter_SAS = ('01DEC2020') %then
      %do;
      %let Quartercal=('Q32020');
   %end;
   %else %if &amp;amp;Quarter_SAS = ('01JAN2021') %then
      %do;
      %let Quartercal=('Q42020');
   %end;
%mend Quarter_SAS;&lt;BR /&gt;%put&amp;nbsp;&amp;amp;Quarter_SAS;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Mar 2020 14:06:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634114#M188198</guid>
      <dc:creator>IgawaKei29</dc:creator>
      <dc:date>2020-03-23T14:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro if then do question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634117#M188200</link>
      <description>&lt;P&gt;Using the DATE9 format with the output of the %SYSFUNC() is going to generate a string that looks like:&lt;/P&gt;
&lt;PRE&gt;01MAR2020&lt;/PRE&gt;
&lt;P&gt;That is NEVER going to match a string that has quotes and parentheses in it like&lt;/P&gt;
&lt;PRE&gt;('01MAR2020')&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Mar 2020 14:12:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634117#M188200</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-03-23T14:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro if then do question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634119#M188201</link>
      <description>&lt;P&gt;I'm not going to re-write your entire macro code, but let me give you some general principles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro variables should not be formatted (except for display in titles or labels or report). So&lt;/P&gt;
&lt;PRE&gt;%let Today_SAS  = %sysfunc(date(),Date9.);&lt;/PRE&gt;
&lt;P&gt;really ought to be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Today_SAS  = %sysfunc(date());&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;for any logical or arithmetical use, which is what you appear to be doing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next, macro variable values almost never need quotes around them, or parentheses around them. You spend a lot of time formatting and unformatting and adding quotes and then removing them. This is all unnecessary.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The exception is when you need to hard-code a specific date, such as you have 01MAR2020. Then you can use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; %if &amp;amp;Quarter_SAS = %sysevalf('01MAR2020'd) %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Really, start from scratch, re-write your code without formatting and without quotes and parentheses, following the above principles, and it will work a lot better with a lot less effort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2020 14:15:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634119#M188201</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-23T14:15:06Z</dc:date>
    </item>
    <item>
      <title>Re: Macro if then do question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634124#M188204</link>
      <description>&lt;P&gt;To add some detail to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;'s comment, this syntax is inaccurate:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   %if &amp;amp;Quarter_SAS = ('01MAR2020') %then
      %do;
      %let Quartercal=('Q12020');
      %end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Macro language automatically treats characters as if they are characters.&amp;nbsp; So this would be better:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   %if &amp;amp;Quarter_SAS = 01MAR2020 %then
      %do;
      %let Quartercal=Q12020;
      %end;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Mar 2020 14:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634124#M188204</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2020-03-23T14:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: Macro if then do question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634128#M188207</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;,&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;.&amp;nbsp; This macro logic is really interesting to me and I can see where formatting and characters can get in the way when trying to code the solution.&amp;nbsp; Using a lot of these %put statements seem to help so I can see the output in my log to help formulate what I need.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appreciate the help again!&amp;nbsp; Thanks &lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2020 14:40:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634128#M188207</guid>
      <dc:creator>IgawaKei29</dc:creator>
      <dc:date>2020-03-23T14:40:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro if then do question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634131#M188208</link>
      <description>&lt;P&gt;Adding to my above example&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let Today_SAS  = %sysfunc(date());&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to see that this is correctly computing the date (and of course you can use this in more complicated date examples, such as with %sysfunc(intnx()) as well)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put &amp;amp;=today_sas %sysfunc(putn(&amp;amp;today_sas,date7.));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Mar 2020 14:42:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634131#M188208</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-03-23T14:42:53Z</dc:date>
    </item>
    <item>
      <title>Re: Macro if then do question</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634216#M188234</link>
      <description>&lt;P&gt;Another question that may be worth considering is does this &lt;STRONG&gt;need&lt;/STRONG&gt; be all macro code?&lt;/P&gt;
&lt;P&gt;The comparisons and calculations may be easier in a DATA _NULL_ step that creates the needed macro values&amp;nbsp; with call symputx.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Mar 2020 18:56:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-if-then-do-question/m-p/634216#M188234</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-03-23T18:56:33Z</dc:date>
    </item>
  </channel>
</rss>

