<?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: Create Date Field in Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884591#M349480</link>
    <description>&lt;P&gt;You are not, repeat &lt;STRONG&gt;not&lt;/STRONG&gt;, passing anything resembling a valid date in those shown values for the parameters Start_dt and End_dt.&lt;/P&gt;
&lt;P&gt;Any place you want to use something passed as '2022-10-27' you want have to convert it to date using the input function with the yymmdd10 informat.&lt;/P&gt;</description>
    <pubDate>Wed, 12 Jul 2023 23:43:56 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2023-07-12T23:43:56Z</dc:date>
    <item>
      <title>Create Date Field in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884588#M349477</link>
      <description>&lt;P&gt;How can I assign a specific date as a new field in a macro? I'm trying to create a date field, but have it run as a nested macro so that I can run multiple at a time.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could run it in a data step like this:&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;new_date_field = '27OCT2022'd;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Rather than having this data step in my macro, I'd prefer to assign the date field value to the macro like below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%macro data_pull(yyyy,start_dt,end_dt,new_date_field)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data new;&lt;/P&gt;
&lt;P&gt;set want;&lt;/P&gt;
&lt;P&gt;days = intck('days',&amp;amp;new_date_field.,end_dt);&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;%mend;&lt;/P&gt;
&lt;P&gt;%data_pull(2022, '2022-10-27', '2022-10-31','27OCT2022'd);&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do I assign the last variable in my 'data pull' macro so that it creates a field with October 27th 2022 as the value?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 21:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884588#M349477</guid>
      <dc:creator>Sas_noob25</dc:creator>
      <dc:date>2023-07-12T21:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create Date Field in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884589#M349478</link>
      <description>&lt;P&gt;Use &lt;STRIKE&gt;DATA&lt;/STRIKE&gt; DATE literals, which are expressed either as&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; '27oct2022'd&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;&amp;nbsp; "27oct2022"d.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The latter honors macro values between the quotes, the earlier does not.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  end_dt='31DEC2022'd;
  format end_dt date9. ;
run;


%macro data_pull(yyyy,start_dt,end_dt,new_date_field);
  data new;
    set want;
    days = intck('days',"&amp;amp;new_date_field"d,end_dt);
    put days=;
  run;
%mend;

options mprint;
%data_pull(2022,'2022-10-27','2022-10-31',27OCT2022); &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;See the double-quoted argument of the INTCK function.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would do the same with your start_dt and end_dt parameters in the DATA_PULL macro definition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 22:18:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884589#M349478</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-12T22:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: Create Date Field in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884590#M349479</link>
      <description>&lt;P&gt;Not really clear what you are doing with this macro. &amp;amp;YYYY and &amp;amp;START_DT are never used in the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But ignoring that, &amp;amp;END_DT must be in the proper date literal format for anything to recognize it as a date. '2022-10-31' is a text string, SAS will never recognize it as a date. SAS does recognize '31OCT2022'd as a date literal if you use if properly. Note: date literals are &lt;FONT color="#FF0000"&gt;&lt;EM&gt;always&lt;/EM&gt; &lt;/FONT&gt;in the form 'ddmonyyyy'd (case insensitive, and matched double quotes are allowed) and &lt;FONT color="#FF0000"&gt;&lt;EM&gt;never&lt;/EM&gt; &lt;/FONT&gt;anything else (so 'yyyy-mm-dd'd is invalid and will not work).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So this will work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro data_pull(end_dt,new_date_field);
data new;
    days = intck('days',&amp;amp;new_date_field,&amp;amp;end_dt);
run;
%mend;

%data_pull('31OCT2022'd,'27OCT2022'd)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note taht I have corrected some typos (missing semicolons, missing ampersands) and removed an unnecessary SET statement (unnecessary in this context).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, no macro needed here, just the macro variables are needed, this is much simpler.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let new_date_field='31OCT2022'd;
%let end_dt='27OCT2022'd;

data new;
    days = &amp;amp;new_date_field-&amp;amp;end_dt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 22:32:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884590#M349479</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-12T22:32:56Z</dc:date>
    </item>
    <item>
      <title>Re: Create Date Field in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884591#M349480</link>
      <description>&lt;P&gt;You are not, repeat &lt;STRONG&gt;not&lt;/STRONG&gt;, passing anything resembling a valid date in those shown values for the parameters Start_dt and End_dt.&lt;/P&gt;
&lt;P&gt;Any place you want to use something passed as '2022-10-27' you want have to convert it to date using the input function with the yymmdd10 informat.&lt;/P&gt;</description>
      <pubDate>Wed, 12 Jul 2023 23:43:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884591#M349480</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-07-12T23:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Create Date Field in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884730#M349534</link>
      <description>&lt;P&gt;Thanks, this worked. The start/end date were just fields formatted that way. Thanks for calling that out though.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2023 22:38:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884730#M349534</guid>
      <dc:creator>Sas_noob25</dc:creator>
      <dc:date>2023-07-13T22:38:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create Date Field in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884833#M349586</link>
      <description>&lt;P&gt;But that is IS what your current code is doing.&lt;/P&gt;
&lt;P&gt;You are passing the string&amp;nbsp;&lt;FONT face="courier new,courier"&gt;'27OCT2022'd&lt;/FONT&gt; as the value of the&amp;nbsp;new_date_field parameter of your %DATA_PULL() macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that the call is passing string literals as the values of the other two "date" parameters, instead of a date literal like the last parameter.&amp;nbsp; If you needed to convert those strings into date values you would need to include an INPUT() function call in your code.&amp;nbsp; For example to calculate how many days from start to end you would have to use something like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_range=input(&amp;amp;end_dt,yymmdd10.) - input(&amp;amp;start_dt,yymmdd10.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Which will work because you included the quotes into the values of START_DT and END_DT .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;NOTE: The macro call is much clearer if you include the parameter names in the call, especially if the macro has more than one parameter.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%data_pull
(yyyy=2022
,start_dt='2022-10-27'
,end_dt='2022-10-31'
,new_date_field='27OCT2022'd
); 
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;SAS does not care if you defined the macro to allow the values to be passed by position or not.&amp;nbsp; You can still pass them by name when you call the macro.&amp;nbsp; You just cannot do the reverse: define the macro to require the values to be passed by name and then attempt to call it with the values passed by position.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jul 2023 17:05:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-Date-Field-in-Macro/m-p/884833#M349586</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-07-14T17:05:45Z</dc:date>
    </item>
  </channel>
</rss>

