<?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: Use of Dates in Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894426#M353314</link>
    <description>&lt;P&gt;Please do not post the question and details in a text box. Or if you must force line breaks. It is extremely hard to read long lines of narrative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't include a log with the problem as far as I can see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You state:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;.1. When i run the loop from %DO i = 0 %TO   &amp;amp;RUN_DAYS. -1 ;I starts from 2 the dates are calculated as 02Aug2023 instead of 01Aug2023,&lt;/LI-CODE&gt;
&lt;P&gt;when I run your code with the i= 0 to 2 the first one I get is "DAYS0 = 20230801" not 20230802. You show a value that you are not actually generating as far as I can tell. You should state stuff that actually appears in our log.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;So I am actually sure of your problem.&lt;/P&gt;
&lt;P&gt;STATE the base date you want the program to run on. Code with TODAY &lt;STRONG&gt;changes&lt;/STRONG&gt; values.&lt;/P&gt;
&lt;P&gt;Show the list of values you want to generate when the date is that AND in which variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When we get things in understandable form I won't be surprised if part of the issue isn't this:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;C_MTH = STRIP(INTNX('MONTH',TODAY(),-1));&lt;/LI-CODE&gt;
&lt;P&gt;Why are converting that to date value to character???? You are repeatedly using it as as the argument to INTNX so SAS has to convert C_mth from character back to numeric. Do that often enough with enough variables and you will encounter something where the conversion may not be correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are calling STRIP so many times because of leading spaces after use of PUT then you can avoid it by using -L to justify the PUT&lt;/P&gt;
&lt;P&gt;Instead of&lt;/P&gt;
&lt;PRE&gt;CATT(STRIP(PUT(INTNX('MONTH',C_MTH,0),MONNAME3.)))&lt;/PRE&gt;
&lt;P&gt;use&lt;/P&gt;
&lt;PRE&gt;PUT(INTNX('MONTH',C_MTH,0),MONNAME3. -L)&lt;/PRE&gt;
&lt;P&gt;You are not providing a second function to CATT so it is basically wasting clock cycles and makes code hard to read.&lt;/P&gt;
&lt;P&gt;Same with you don't have second arguement so 'RMTH' is sufficient.&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;CATS('RMTH')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution: You should not use the *&amp;lt;code&amp;gt; ; form of comments inside of macro definitions. The "comment" depending on the actual content may do interesting things. Macro language comments use %*&amp;lt;code&amp;gt;; or the /*&amp;lt;code&amp;gt;*/ structure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you have that data _null_ inside of the macro loop? Creating macro variables just to make a single assignment in another data step is a waste of clock cycles and makes for hard to read and follow code.&lt;/P&gt;</description>
    <pubDate>Fri, 15 Sep 2023 04:54:06 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-09-15T04:54:06Z</dc:date>
    <item>
      <title>Use of Dates in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894422#M353310</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Hi All,&lt;BR /&gt;&lt;BR /&gt;I am trying to Automate a code to extract daily data for example "01Aug2023 - 02Aug2023" from teradata. &lt;BR /&gt;i have the below code which is working, but i have 2 issues , Which i am not able to resolve, kindly suggest.&lt;BR /&gt;1.&lt;BR /&gt; When i run the loop from %DO i = 0 %TO   &amp;amp;RUN_DAYS. -1 ;&lt;BR /&gt;I starts from 2 the dates are calculated as 02Aug2023 instead of 01Aug2023, but when i use&lt;BR /&gt;%DO i = 0 %TO  %EVAL(&amp;amp;RUN_DAYS. -1);&lt;BR /&gt;the dates are calculated  day0 = 01Aug2023 and so on....&lt;BR /&gt;2. I am not able to use the dates while extraction data from teradata table.&lt;BR /&gt;&lt;BR /&gt;I am able to use the dates as mentioned below, which would include duplicate data.&lt;BR /&gt;start date End Date&lt;BR /&gt; 20230802  20230803 &lt;BR /&gt; 20230803  20230804&lt;BR /&gt; 20230804  20230805&lt;BR /&gt;&lt;BR /&gt;but was trying to have the dates as mentioned below&lt;BR /&gt;start date End Date&lt;BR /&gt; 20230802  20230803 &lt;BR /&gt; 20230804  20230805&lt;BR /&gt; 20230806  20230807&lt;BR /&gt;&lt;BR /&gt;I can extract the data for each day using the above code, as i don't want make changes to the Already running code in production.&lt;BR /&gt;Please find the logs and the code attached.&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _NULL_;
C_MTH = STRIP(INTNX('MONTH',TODAY(),-1)); /*REPORTING MONTH IS AUGUST2023 OR ANY MONTH PREVIOUS TO THE CURRENT MONTH*/
CALL SYMPUT (CATS('RMTH'),CATT(STRIP(PUT(INTNX('MONTH',C_MTH,0),MONNAME3.)))); /*ONLY MONTH NAME FOR REPORTING PURPOSES*/
CALL SYMPUT (CATS('REP_MTH'),CATT(LOWCASE(PUT(INTNX('MONTH',C_MTH,0),MONYY7.))) );/*ONLY MONTH &amp;amp; YEAR FORMAT REPORTING PURPOSES*/
CALL SYMPUT (CATS('ACT_MTH'),CATT(LOWCASE(STRIP(PUT(INTNX('MONTH',C_MTH,0),MONNAME3.))),STRIP(PUT(INTNX('YEAR',C_MTH,0),YEAR2.)))) ; /*ONLY MONTH &amp;amp; YEAR FORMAT REPORTING PURPOSES*/
CALL SYMPUT (CATS('PERIOD'),PUT(INTNX('MONTH',TODAY(),-1),YYMMDDN6.));/*DATE FORMAT FOR EXTRACTION*/
CALL SYMPUT (('RUN_DAYS'),PUT(DAY(INTNX('MONTH',C_MTH,0,'E')),Z2.)); /*NUMBER OF DAYS OF THE MONTH*/
RUN;
%PUT "RMTH &amp;amp;RMTH.";
%PUT "REP_MTH &amp;amp;REP_MTH.";
%PUT "ACT_MTH &amp;amp;ACT_MTH.";
%PUT "PERIOD &amp;amp;PERIOD.";
%PUT "RUN_DAYS &amp;amp;RUN_DAYS.";


%MACRO DATA_PULL();
%DO i = 0 %TO 2; *%EVAL(&amp;amp;RUN_DAYS. -1);
%LET MTH&amp;amp;i. = %SYSFUNC(PUTN(&amp;amp;i.,Z2.));
DATA _NULL_;
C_MTH = STRIP(INTNX('MONTH',TODAY(),-1)); 
CALL SYMPUT (CATS("TDY&amp;amp;i."),STRIP(PUT(INTNX('DAY',(INTNX('MONTH',C_MTH,0)),&amp;amp;i.),YYMMDDN8.)));
 CALL SYMPUT (CATS("PTDY&amp;amp;i."),STRIP(PUT(INTNX('DAY',(INTNX('MONTH',C_MTH,0)),%EVAL(&amp;amp;i.+1)),YYMMDDN8.))); 
RUN;

%PUT "DAYS&amp;amp;i. = &amp;amp;&amp;amp;TDY&amp;amp;i.";
%PUT "PTDAYS&amp;amp;i. = &amp;amp;&amp;amp;PTDY&amp;amp;i."; 

/*WE ARE EXTRACTING DATA FROM TERADATA SALES TABLE. UISNG BETWEEN START DATE AND END DATE FOR A PERIOD OF EXTRACTION.*/

DATA ST&amp;amp;i.; 
 STDTE 	= "&amp;amp;&amp;amp;TDY&amp;amp;i."; 
 ENDDTE = "&amp;amp;&amp;amp;PTDY&amp;amp;i."; 
RUN; 


%END;

%MEND;
%DATA_PULL();

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE id="pre_sasLog_372544" class="sasLog" style="background-color: transparent; -webkit-user-select: text; -webkit-user-modify: read-only; -webkit-touch-callout: default; border-top-width: 0px; border-bottom-width: 0px;"&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="sasSource"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE id="pre_sasLog_372544" class="sasLog" style="background-color: transparent; -webkit-user-select: text; -webkit-user-modify: read-only; -webkit-touch-callout: default; border-top-width: 0px; border-bottom-width: 0px;"&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&amp;nbsp;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA _NULL_; C_MTH = STRIP(INTNX('MONTH',TODAY(),-1)); /*REPORTING MONTH IS AUGUST2023 OR ANY MONTH PREVIOUS TO THE CURRENT MONTH*/ CALL SYMPUT (CATS('RMTH'),CATT(STRIP(PUT(INTNX('MONTH',C_MTH,0),MONNAME3.)))); /*ONLY MONTH NAME FOR REPORTING PURPOSES*/ CALL SYMPUT (CATS('REP_MTH'),CATT(LOWCASE(PUT(INTNX('MONTH',C_MTH,0),MONYY7.))) );/*ONLY MONTH &amp;amp; YEAR FORMAT REPORTING PURPOSES*/ CALL SYMPUT (CATS('ACT_MTH'),CATT(LOWCASE(STRIP(PUT(INTNX('MONTH',C_MTH,0),MONNAME3.))),STRIP(PUT(INTNX('YEAR',C_MTH,0),YEAR2.)))) ; /*ONLY MONTH &amp;amp; YEAR FORMAT REPORTING PURPOSES*/ CALL SYMPUT (CATS('PERIOD'),PUT(INTNX('MONTH',TODAY(),-1),YYMMDDN6.));/*DATE FORMAT FOR EXTRACTION*/ CALL SYMPUT (('RUN_DAYS'),PUT(DAY(INTNX('MONTH',C_MTH,0,'E')),Z2.)); /*NUMBER OF DAYS OF THE MONTH*/ RUN; %PUT "RMTH &amp;amp;RMTH."; %PUT "REP_MTH &amp;amp;REP_MTH."; %PUT "ACT_MTH &amp;amp;ACT_MTH."; %PUT "PERIOD &amp;amp;PERIOD."; %PUT "RUN_DAYS &amp;amp;RUN_DAYS."; %MACRO DATA_PULL(); %DO i = 0 %TO 2; *%EVAL(&amp;amp;RUN_DAYS. -1); %LET MTH&amp;amp;i. = %SYSFUNC(PUTN(&amp;amp;i.,Z2.)); DATA _NULL_; C_MTH = STRIP(INTNX('MONTH',TODAY(),-1)); CALL SYMPUT (CATS("TDY&amp;amp;i."),STRIP(PUT(INTNX('DAY',(INTNX('MONTH',C_MTH,0)),&amp;amp;i.),YYMMDDN8.))); CALL SYMPUT (CATS("PTDY&amp;amp;i."),STRIP(PUT(INTNX('DAY',(INTNX('MONTH',C_MTH,0)),%EVAL(&amp;amp;i.+1)),YYMMDDN8.))); RUN; %PUT "DAYS&amp;amp;i. = &amp;amp;&amp;amp;TDY&amp;amp;i."; %PUT "PTDAYS&amp;amp;i. = &amp;amp;&amp;amp;PTDY&amp;amp;i."; /*WE ARE EXTRACTING DATA FROM TERADATA SALES TABLE. UISNG BETWEEN START DATE AND END DATE FOR A PERIOD OF EXTRACTION.*/ DATA ST&amp;amp;i.; STDTE = "&amp;amp;&amp;amp;TDY&amp;amp;i."; ENDDTE = "&amp;amp;&amp;amp;PTDY&amp;amp;i."; RUN; %END; %MEND; %DATA_PULL(); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 02:31:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894422#M353310</guid>
      <dc:creator>Santt0sh</dc:creator>
      <dc:date>2023-09-15T02:31:50Z</dc:date>
    </item>
    <item>
      <title>Re: Use of Dates in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894426#M353314</link>
      <description>&lt;P&gt;Please do not post the question and details in a text box. Or if you must force line breaks. It is extremely hard to read long lines of narrative.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't include a log with the problem as far as I can see.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You state:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;.1. When i run the loop from %DO i = 0 %TO   &amp;amp;RUN_DAYS. -1 ;I starts from 2 the dates are calculated as 02Aug2023 instead of 01Aug2023,&lt;/LI-CODE&gt;
&lt;P&gt;when I run your code with the i= 0 to 2 the first one I get is "DAYS0 = 20230801" not 20230802. You show a value that you are not actually generating as far as I can tell. You should state stuff that actually appears in our log.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;So I am actually sure of your problem.&lt;/P&gt;
&lt;P&gt;STATE the base date you want the program to run on. Code with TODAY &lt;STRONG&gt;changes&lt;/STRONG&gt; values.&lt;/P&gt;
&lt;P&gt;Show the list of values you want to generate when the date is that AND in which variables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When we get things in understandable form I won't be surprised if part of the issue isn't this:&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;C_MTH = STRIP(INTNX('MONTH',TODAY(),-1));&lt;/LI-CODE&gt;
&lt;P&gt;Why are converting that to date value to character???? You are repeatedly using it as as the argument to INTNX so SAS has to convert C_mth from character back to numeric. Do that often enough with enough variables and you will encounter something where the conversion may not be correct.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are calling STRIP so many times because of leading spaces after use of PUT then you can avoid it by using -L to justify the PUT&lt;/P&gt;
&lt;P&gt;Instead of&lt;/P&gt;
&lt;PRE&gt;CATT(STRIP(PUT(INTNX('MONTH',C_MTH,0),MONNAME3.)))&lt;/PRE&gt;
&lt;P&gt;use&lt;/P&gt;
&lt;PRE&gt;PUT(INTNX('MONTH',C_MTH,0),MONNAME3. -L)&lt;/PRE&gt;
&lt;P&gt;You are not providing a second function to CATT so it is basically wasting clock cycles and makes code hard to read.&lt;/P&gt;
&lt;P&gt;Same with you don't have second arguement so 'RMTH' is sufficient.&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;CATS('RMTH')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Caution: You should not use the *&amp;lt;code&amp;gt; ; form of comments inside of macro definitions. The "comment" depending on the actual content may do interesting things. Macro language comments use %*&amp;lt;code&amp;gt;; or the /*&amp;lt;code&amp;gt;*/ structure.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you have that data _null_ inside of the macro loop? Creating macro variables just to make a single assignment in another data step is a waste of clock cycles and makes for hard to read and follow code.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 04:54:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894426#M353314</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-09-15T04:54:06Z</dc:date>
    </item>
    <item>
      <title>Re: Use of Dates in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894444#M353321</link>
      <description>&lt;P&gt;Re-formatting your question so it is readable would be very helpful. And it would also probably get you faster answers. As stated by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt; , do not type your question in a code box.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 09:47:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894444#M353321</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-09-15T09:47:02Z</dc:date>
    </item>
    <item>
      <title>Re: Use of Dates in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894472#M353327</link>
      <description>&lt;P&gt;Don't overcomplicate things:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
c_mth = intnx('month',today(),-1));
call symputx('RMTH',put(c_mth,,monname3.));
call symputx('REP_MTH',lowcase(put(c_mth,monyy7.)));
call symputx('ACT_MTH',lowcase(put(c_mnth,monname3.))!!PUT(c_mth,year2.));
call symputx('PERIOD',put(c_mth,yymmddn6.));
call symputx('RUN_DAYS',put(day(intnx('month',c_mth,0,'e')),z2.));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;CALL SYMPUTX will automatically strip the second argument.&lt;/P&gt;
&lt;P&gt;INTNX with a third argument of 0 will result in the same value ("beginning" alignment).&lt;/P&gt;</description>
      <pubDate>Fri, 15 Sep 2023 12:48:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Use-of-Dates-in-Macro/m-p/894472#M353327</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-09-15T12:48:11Z</dc:date>
    </item>
  </channel>
</rss>

