<?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: Date in macro variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801942#M315653</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Subtract 7 from the date. To make (integer) calculations in macro language, use the %EVAL function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that if the expression is being passed as an argument to a SAS function via %SYSFUNC() you don't need to use %EVAL() to do the calculations before hand.&lt;/P&gt;
&lt;PRE&gt;338   %let x1=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);
339   %let x2=%sysfunc(week(%sysfunc(today())-7),z2.);
340
341   %put &amp;amp;=x1 &amp;amp;=x2;
X1=10 X2=10
&lt;/PRE&gt;
&lt;P&gt;A NUMERIC argument that is.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;346   %put |%sysfunc(putn(5+6,Z5.))|;
|00011|
347   %put |%sysfunc(putc(5+6,$5.-R))|;
|  5+6|
&lt;/LI-CODE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And - tadaa! - once again I learned something new here.&lt;/P&gt;</description>
    <pubDate>Mon, 14 Mar 2022 14:26:20 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2022-03-14T14:26:20Z</dc:date>
    <item>
      <title>Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801601#M315483</link>
      <description>&lt;P&gt;Hi All,&lt;BR /&gt;I require a small assistance from you guys regarding.&lt;BR /&gt;I would like to have my table name embedded with week of today and year of today.&lt;BR /&gt;I.e myfile102022&lt;BR /&gt;I tried everything like &lt;BR /&gt;Data myfile&amp;amp;d._&amp;amp;d1.;&lt;BR /&gt;%let d=week(today());&lt;BR /&gt;%let d1=year(today());&lt;BR /&gt;%put &amp;amp;d &amp;amp;d1;&lt;BR /&gt;run;&lt;BR /&gt;But the it isn't printing the values of d and d1 instead it is taking the values as is i.e&lt;BR /&gt;myfileweek(today())_year(today())&lt;BR /&gt;Any help would be greatly appreciated. Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 06:57:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801601#M315483</guid>
      <dc:creator>Pandu2</dc:creator>
      <dc:date>2022-03-11T06:57:57Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801602#M315484</link>
      <description>&lt;P&gt;Macro statements like %LET and macro variables are resolved while the code is fetched for compiling/execution, so your code is equivalent to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=week(today());
%let d1=year(today());
%put &amp;amp;d &amp;amp;d1;

Data myfile&amp;amp;d._&amp;amp;d1.;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and that is the correct sequence for writing macro code.&lt;/P&gt;
&lt;P&gt;You store the&amp;nbsp;&lt;U&gt;text&lt;/U&gt; "week(today())" in your macro variable d, and characters like parentheses are clearly not allowed in dataset names. The content of the macro variables is visible in your %PUT.&lt;/P&gt;
&lt;P&gt;To &lt;EM&gt;evaluate&lt;/EM&gt; data step functions while the %LET is executed, you need to wrap them in %SYSFUNC:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=%sysfunc(week(%sysfunc(today()),z2.);
%let d1=%sysfunc(year(%sysfunc(today()));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I added the Z2. format to the first%SYSFUNC so that week 9 (09) comes before week 10.&lt;/P&gt;
&lt;P&gt;And for better handling, use the correct hierarchical order in your dataset name:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myfile&amp;amp;d1._&amp;amp;d.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Mar 2022 06:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801602#M315484</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-11T06:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801605#M315486</link>
      <description>Does macro variables work under proc SQL?. And also got the error like.&lt;BR /&gt;Libref myfile is not assigned even though it isn't a library. Any advice please</description>
      <pubDate>Fri, 11 Mar 2022 07:12:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801605#M315486</guid>
      <dc:creator>Pandu2</dc:creator>
      <dc:date>2022-03-11T07:12:32Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801607#M315487</link>
      <description>&lt;P&gt;I see I missed parentheses. See this updated example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=%sysfunc(week(%sysfunc(today())),z2.);
%let d1=%sysfunc(year(%sysfunc(today())));

data myfile&amp;amp;d1._&amp;amp;d.;
set sashelp.class;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which gets this log:&lt;/P&gt;
&lt;PRE&gt; 69         %let d=%sysfunc(week(%sysfunc(today())),z2.);
 70         %let d1=%sysfunc(year(%sysfunc(today())));
 71         
 72         data myfile&amp;amp;d1._&amp;amp;d.;
 73         set sashelp.class;
 74         run;
 
 NOTE: There were 19 observations read from the data set SASHELP.CLASS.
 NOTE: The data set WORK.MYFILE2022_10 has 19 observations and 5 variables.
 &lt;/PRE&gt;
&lt;P&gt;And yes, you can use macro variables in all SAS code, because the macro preprocessor works with the code &lt;U&gt;text&lt;/U&gt; before it is sent to execute. So the macro variables will be resolved before the SQL code is sent to the SQL interpreter.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 07:37:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801607#M315487</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-11T07:37:17Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801609#M315488</link>
      <description>Thanks a bunch it is working now and I kept work library before the filename so that it didn't give the error which I previously got. Can't be thankful enough.</description>
      <pubDate>Fri, 11 Mar 2022 07:32:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801609#M315488</guid>
      <dc:creator>Pandu2</dc:creator>
      <dc:date>2022-03-11T07:32:33Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801626#M315496</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/405593"&gt;@Pandu2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thanks a bunch it is working now and I kept work library before the filename so that it didn't give the error which I previously got. Can't be thankful enough.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/405593"&gt;@Pandu2&lt;/a&gt;&amp;nbsp;Then mark the answer that was most helpful to you as solution.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 09:52:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801626#M315496</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-11T09:52:15Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801895#M315624</link>
      <description>Hii,&lt;BR /&gt;In your code&lt;BR /&gt;%let d=%sysfunc(week(%sysfunc(today())),z2.).&lt;BR /&gt; I tried to get the prior week of it but couldn't figure out the best. Could you please help me on that.&lt;BR /&gt;Thanks</description>
      <pubDate>Mon, 14 Mar 2022 06:45:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801895#M315624</guid>
      <dc:creator>Pandu2</dc:creator>
      <dc:date>2022-03-14T06:45:43Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801897#M315625</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/405593"&gt;@Pandu2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hii,&lt;BR /&gt;In your code&lt;BR /&gt;%let d=%sysfunc(week(%sysfunc(today())),z2.).&lt;BR /&gt;I tried to get the prior week of it but couldn't figure out the best. Could you please help me on that.&lt;BR /&gt;Thanks&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=%sysfunc(week(%sysfunc(today())),z2.);
%put &amp;amp;=d;

%let d2=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);
%put &amp;amp;=d2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1647240862060.png" style="width: 546px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69433i974F2994ED6AE094/image-dimensions/546x144?v=v2" width="546" height="144" role="button" title="Patrick_0-1647240862060.png" alt="Patrick_0-1647240862060.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 06:54:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801897#M315625</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-14T06:54:33Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801898#M315626</link>
      <description>&lt;P&gt;Subtract 7 from the date. To make (integer) calculations in macro language, use the %EVAL function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 14 Mar 2022 06:58:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801898#M315626</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-14T06:58:47Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801900#M315628</link>
      <description>Thankyou for your assistance. The week represents Fiscal week but not sure subtracting -7 give the same until it hits the next week. Please do check by giving tomorrow's date instead of today's.&lt;BR /&gt;Thanks.</description>
      <pubDate>Mon, 14 Mar 2022 07:04:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801900#M315628</guid>
      <dc:creator>Pandu2</dc:creator>
      <dc:date>2022-03-14T07:04:28Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801901#M315629</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/405593"&gt;@Pandu2&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thankyou for your assistance. The week represents Fiscal week but not sure subtracting -7 give the same until it hits the next week. Please do check by giving tomorrow's date instead of today's.&lt;BR /&gt;Thanks.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/405593"&gt;@Pandu2&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The source value created by the today() function is a count of days since 1/1/1960.&lt;/P&gt;
&lt;P&gt;If the week() function returns the correct value then substracting 7 days from the source value before applying the week() function will work.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 07:07:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801901#M315629</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-14T07:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801904#M315632</link>
      <description>Thankyou</description>
      <pubDate>Mon, 14 Mar 2022 07:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801904#M315632</guid>
      <dc:creator>Pandu2</dc:creator>
      <dc:date>2022-03-14T07:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801905#M315633</link>
      <description>Thanks a ton</description>
      <pubDate>Mon, 14 Mar 2022 07:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801905#M315633</guid>
      <dc:creator>Pandu2</dc:creator>
      <dc:date>2022-03-14T07:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801910#M315636</link>
      <description>&lt;P&gt;Which values do you expect then for the dates from 2022-03-14 to 2022-03-21?&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 09:48:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801910#M315636</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-14T09:48:07Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801933#M315651</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Subtract 7 from the date. To make (integer) calculations in macro language, use the %EVAL function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that if the expression is being passed as an argument to a SAS function via %SYSFUNC() you don't need to use %EVAL() to do the calculations before hand.&lt;/P&gt;
&lt;PRE&gt;338   %let x1=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);
339   %let x2=%sysfunc(week(%sysfunc(today())-7),z2.);
340
341   %put &amp;amp;=x1 &amp;amp;=x2;
X1=10 X2=10
&lt;/PRE&gt;
&lt;P&gt;A NUMERIC argument that is.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;346   %put |%sysfunc(putn(5+6,Z5.))|;
|00011|
347   %put |%sysfunc(putc(5+6,$5.-R))|;
|  5+6|
&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 14 Mar 2022 13:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801933#M315651</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-14T13:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: Date in macro variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801942#M315653</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Subtract 7 from the date. To make (integer) calculations in macro language, use the %EVAL function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let d=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Note that if the expression is being passed as an argument to a SAS function via %SYSFUNC() you don't need to use %EVAL() to do the calculations before hand.&lt;/P&gt;
&lt;PRE&gt;338   %let x1=%sysfunc(week(%eval(%sysfunc(today())-7)),z2.);
339   %let x2=%sysfunc(week(%sysfunc(today())-7),z2.);
340
341   %put &amp;amp;=x1 &amp;amp;=x2;
X1=10 X2=10
&lt;/PRE&gt;
&lt;P&gt;A NUMERIC argument that is.&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;346   %put |%sysfunc(putn(5+6,Z5.))|;
|00011|
347   %put |%sysfunc(putc(5+6,$5.-R))|;
|  5+6|
&lt;/LI-CODE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And - tadaa! - once again I learned something new here.&lt;/P&gt;</description>
      <pubDate>Mon, 14 Mar 2022 14:26:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Date-in-macro-variables/m-p/801942#M315653</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-03-14T14:26:20Z</dc:date>
    </item>
  </channel>
</rss>

