<?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 How to write a macro to run a code every Monday of every month? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798331#M313847</link>
    <description>&lt;P&gt;I have a block of code which extracts customers who had made a transfer in the last month.&amp;nbsp; This macro is currently monthly and runs once a month.&lt;/P&gt;
&lt;P&gt;Now, I want to run this code on every Monday for the last 4 months.&lt;/P&gt;
&lt;P&gt;How can I use a macro to achieve this and convert this exercise from monthly to weekly so that this macro runs on every Monday and we get the list of customers who made a transfer in last 7 days?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%LET Mon_ST = 202111;			
%LET Mon_EN = 202201;
	
Option obs = Max;			
			
%macro copy_data();

%DO Mnth = &amp;amp;Mon_ST. %TO &amp;amp;Mon_EN. ;			
%IF %SUBSTR(&amp;amp;Mnth.,5,2) = 13 %THEN %LET Mnth =	%EVAL(%SUBSTR(&amp;amp;Mnth.,1,4)+1)01;	
%let YYYYMMDD =	%SYSFUNC(INTNX(Month,"%SYSFUNC(MDY(%SUBSTR(&amp;amp;Mnth.,5,2),01,%SUBSTR(&amp;amp;Mnth.,1,4)),DATE9.)"D,0,E),YYMMddN8.);
	
	%Let Track_strt=%SYSFUNC(INTNX(Day,"%SYSFUNC(MDY(%SUBSTR(&amp;amp;YYYYMMDD.,5,2),%SUBSTR(&amp;amp;YYYYMMDD.,7,2),%SUBSTR(&amp;amp;YYYYMMDD.,1,4)),DATE9.)"D,-30,B),YYMMDDN8.);
	%Let Track_endt=%SYSFUNC(INTNX(Day,"%SYSFUNC(MDY(%SUBSTR(&amp;amp;YYYYMMDD.,5,2),%SUBSTR(&amp;amp;YYYYMMDD.,7,2),%SUBSTR(&amp;amp;YYYYMMDD.,1,4)),DATE9.)"D,0,E),YYMMDDN8.);

%Put  &amp;amp;Track_endt. &amp;amp;Track_strt.;	

/***** Block of Code extracting transfer history information in last 1 month *****/

%end;			

%mend;			
%copy_data();	&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 24 Feb 2022 07:57:13 GMT</pubDate>
    <dc:creator>Shradha1</dc:creator>
    <dc:date>2022-02-24T07:57:13Z</dc:date>
    <item>
      <title>How to write a macro to run a code every Monday of every month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798331#M313847</link>
      <description>&lt;P&gt;I have a block of code which extracts customers who had made a transfer in the last month.&amp;nbsp; This macro is currently monthly and runs once a month.&lt;/P&gt;
&lt;P&gt;Now, I want to run this code on every Monday for the last 4 months.&lt;/P&gt;
&lt;P&gt;How can I use a macro to achieve this and convert this exercise from monthly to weekly so that this macro runs on every Monday and we get the list of customers who made a transfer in last 7 days?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%LET Mon_ST = 202111;			
%LET Mon_EN = 202201;
	
Option obs = Max;			
			
%macro copy_data();

%DO Mnth = &amp;amp;Mon_ST. %TO &amp;amp;Mon_EN. ;			
%IF %SUBSTR(&amp;amp;Mnth.,5,2) = 13 %THEN %LET Mnth =	%EVAL(%SUBSTR(&amp;amp;Mnth.,1,4)+1)01;	
%let YYYYMMDD =	%SYSFUNC(INTNX(Month,"%SYSFUNC(MDY(%SUBSTR(&amp;amp;Mnth.,5,2),01,%SUBSTR(&amp;amp;Mnth.,1,4)),DATE9.)"D,0,E),YYMMddN8.);
	
	%Let Track_strt=%SYSFUNC(INTNX(Day,"%SYSFUNC(MDY(%SUBSTR(&amp;amp;YYYYMMDD.,5,2),%SUBSTR(&amp;amp;YYYYMMDD.,7,2),%SUBSTR(&amp;amp;YYYYMMDD.,1,4)),DATE9.)"D,-30,B),YYMMDDN8.);
	%Let Track_endt=%SYSFUNC(INTNX(Day,"%SYSFUNC(MDY(%SUBSTR(&amp;amp;YYYYMMDD.,5,2),%SUBSTR(&amp;amp;YYYYMMDD.,7,2),%SUBSTR(&amp;amp;YYYYMMDD.,1,4)),DATE9.)"D,0,E),YYMMDDN8.);

%Put  &amp;amp;Track_endt. &amp;amp;Track_strt.;	

/***** Block of Code extracting transfer history information in last 1 month *****/

%end;			

%mend;			
%copy_data();	&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 07:57:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798331#M313847</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2022-02-24T07:57:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a macro to run a code every Monday of every month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798337#M313848</link>
      <description>&lt;P&gt;That's not a macro issue, that's a scheduling issue. On a UNIX SAS server, I would simply use cron/crontab to run the code on the desired days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To limit to data of the last 7 days, use a macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let track_strt = %eval(%sysfunc(today()) - 7);
%let track_endt = %sysfunc(today());&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;These values are the raw SAS date values and can be compared directly to SAS dates in the datasets (no conversion needed). See Maxim 28: Macro Variables Need No Formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Similarly, you use the raw values in calculations:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let mnth=202201;
%let yyyymmdd =	%sysfunc(intnx(month,%sysfunc(inputn(&amp;amp;mnth.01,yymmdd8.)),0,e),yymmddn8.);
%put &amp;amp;yyyymmdd.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also note how I reduced statement complexity by using the INPUTN function instead of MDY and multiple %SUBSTR's.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 08:34:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798337#M313848</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-24T08:34:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a macro to run a code every Monday of every month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798338#M313849</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; Agreed!!. From a futuristic view scheduling this task would help. But if I want to do it retrospectively for the past 3 months, then I would need to create a macro and run it manually. That's where I am getting stuck.</description>
      <pubDate>Thu, 24 Feb 2022 08:37:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798338#M313849</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2022-02-24T08:37:44Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a macro to run a code every Monday of every month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798341#M313851</link>
      <description>&lt;P&gt;So you want weekly data, starting with 15 or 16 weeks back from today?&lt;/P&gt;
&lt;P&gt;First, I would limit the data with a WHERE condition, and secondly, use one of the YYWEEK formats for the dates in the reporting procedures (which then use the formatted values).&lt;/P&gt;
&lt;P&gt;e.g.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=sashelp.citiday mean;
class date;
format date yyweeku7.;
var dcd1m;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can see that the results are grouped by weeks.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 08:50:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798341#M313851</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-02-24T08:50:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a macro to run a code every Monday of every month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798376#M313865</link>
      <description>&lt;P&gt;Instead of messing about with a bunch of %sysfunc and the&amp;nbsp; MDY function with way too many %substr calls try using the INPUTN with the YYMMN informat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;%LET Mon_ST = 202111;

%let date = %sysfunc(inputn(&amp;amp;mon_st.,yymmn6.));

%let textdate= %sysfunc(putn(&amp;amp;date., date9.));

%put Textdate is: &amp;amp;textdate.;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 13:21:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-write-a-macro-to-run-a-code-every-Monday-of-every-month/m-p/798376#M313865</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-02-24T13:21:35Z</dc:date>
    </item>
  </channel>
</rss>

