<?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 Creating macro variables based on conditional logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variables-based-on-conditional-logic/m-p/848217#M335348</link>
    <description>&lt;P&gt;I'm creating macro variables to store dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want these dates to be set to certain days if the code is run on Monday morning. If the code isn't run on Monday morning, I want the dates to be set to different days.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought I was doing this correctly with my code below. When I run my code, my date macro variables are indeed returned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if I change my if statement (e.g. change if weekday = 2 to if weekday = 3) and then run my code, I'm returned dates that are intended to be set if weekday = 2 is returned (i.e. returned dates aren't changed, despite making change to if statement). If I run my code again (without making any changes to code after the initial change of weekday = 2 to weekday = 3), I'm then returned the dates that are set if weekday = 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why do I have to run my code twice after making a change to the if statement for the logic to work and to return the intended dates?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data _null_;

	/*if program runs on Monday before noon, then set reportdate to Saturday and priordate to Friday*/
	if hour(time()) &amp;lt; 12 and weekday(today()) = 2 then do;
		ReportDate  = put(intnx('day',TODAY(),-2),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
		PriorDate   = put(intnx('day',TODAY(),-3),date9.); call symputx('PriorDate',"'"||PriorDate||"'");  
	end;

	/*if program doesn't run Monday morning, then set reportdate to current day and priordate to yesterday*/
	else do;
		ReportDate  = put(intnx('day',TODAY(),0),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
		PriorDate   = put(intnx('day',TODAY(),-1),date9.); call symputx('PriorDate',"'"||PriorDate||"'");  
	end;

	%put &amp;amp;ReportDate; %put &amp;amp;PriorDate;	
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 06 Dec 2022 22:27:11 GMT</pubDate>
    <dc:creator>everyone</dc:creator>
    <dc:date>2022-12-06T22:27:11Z</dc:date>
    <item>
      <title>Creating macro variables based on conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variables-based-on-conditional-logic/m-p/848217#M335348</link>
      <description>&lt;P&gt;I'm creating macro variables to store dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want these dates to be set to certain days if the code is run on Monday morning. If the code isn't run on Monday morning, I want the dates to be set to different days.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought I was doing this correctly with my code below. When I run my code, my date macro variables are indeed returned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, if I change my if statement (e.g. change if weekday = 2 to if weekday = 3) and then run my code, I'm returned dates that are intended to be set if weekday = 2 is returned (i.e. returned dates aren't changed, despite making change to if statement). If I run my code again (without making any changes to code after the initial change of weekday = 2 to weekday = 3), I'm then returned the dates that are set if weekday = 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why do I have to run my code twice after making a change to the if statement for the logic to work and to return the intended dates?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data _null_;

	/*if program runs on Monday before noon, then set reportdate to Saturday and priordate to Friday*/
	if hour(time()) &amp;lt; 12 and weekday(today()) = 2 then do;
		ReportDate  = put(intnx('day',TODAY(),-2),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
		PriorDate   = put(intnx('day',TODAY(),-3),date9.); call symputx('PriorDate',"'"||PriorDate||"'");  
	end;

	/*if program doesn't run Monday morning, then set reportdate to current day and priordate to yesterday*/
	else do;
		ReportDate  = put(intnx('day',TODAY(),0),date9.); call symputx('ReportDate',"'"||ReportDate||"'");
		PriorDate   = put(intnx('day',TODAY(),-1),date9.); call symputx('PriorDate',"'"||PriorDate||"'");  
	end;

	%put &amp;amp;ReportDate; %put &amp;amp;PriorDate;	
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2022 22:27:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variables-based-on-conditional-logic/m-p/848217#M335348</guid>
      <dc:creator>everyone</dc:creator>
      <dc:date>2022-12-06T22:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variables based on conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variables-based-on-conditional-logic/m-p/848221#M335349</link>
      <description>&lt;P&gt;Move your %PUT statements &amp;nbsp;after the RUN.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you place a %PUT statement inside a DATA step, it is execute before the data step code is executed. &amp;nbsp;So there is a timing mistake.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Dec 2022 23:11:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variables-based-on-conditional-logic/m-p/848221#M335349</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2022-12-06T23:11:57Z</dc:date>
    </item>
    <item>
      <title>Re: Creating macro variables based on conditional logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variables-based-on-conditional-logic/m-p/848222#M335350</link>
      <description>&lt;P&gt;What are you using those macro variables for?&lt;/P&gt;
&lt;P&gt;If you are using them to select or compare to other dates then using the formatted values is inefficient.&lt;/P&gt;
&lt;P&gt;If they are being used for things that humans need to see, such as title statements or filenames then the formatting makes sense.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may want to investigate use of :&amp;nbsp; quote(reportdate) instead of, the very ugly,&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=""&gt;"'"||ReportDate||"'"&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Dec 2022 23:17:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-macro-variables-based-on-conditional-logic/m-p/848222#M335350</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-12-06T23:17:06Z</dc:date>
    </item>
  </channel>
</rss>

