BookmarkSubscribeRSS Feed
Obsidian | Level 7

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. 


I could run it in a data step like this:

data want;

set have;

new_date_field = '27OCT2022'd;



Rather than having this data step in my macro, I'd prefer to assign the date field value to the macro like below.


%macro data_pull(yyyy,start_dt,end_dt,new_date_field)


data new;

set want;

days = intck('days',&new_date_field.,end_dt);



%data_pull(2022, '2022-10-27', '2022-10-31','27OCT2022'd); 

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?


Thanks in advance!



Jade | Level 19

Use DATA DATE literals, which are expressed either as





The latter honors macro values between the quotes, the earlier does not.




data want;
  format end_dt date9. ;

%macro data_pull(yyyy,start_dt,end_dt,new_date_field);
  data new;
    set want;
    days = intck('days',"&new_date_field"d,end_dt);
    put days=;

options mprint;

See the double-quoted argument of the INTCK function.



I would do the same with your start_dt and end_dt parameters in the DATA_PULL macro definition.



The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

Obsidian | Level 7

Thanks, this worked. The start/end date were just fields formatted that way. Thanks for calling that out though.

Diamond | Level 26

Not really clear what you are doing with this macro. &YYYY and &START_DT are never used in the macro.


But ignoring that, &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 always in the form 'ddmonyyyy'd (case insensitive, and matched double quotes are allowed) and never anything else (so 'yyyy-mm-dd'd is invalid and will not work).


So this will work:


%macro data_pull(end_dt,new_date_field);
data new;
    days = intck('days',&new_date_field,&end_dt);



Please note taht I have corrected some typos (missing semicolons, missing ampersands) and removed an unnecessary SET statement (unnecessary in this context).


Also, no macro needed here, just the macro variables are needed, this is much simpler.


%let new_date_field='31OCT2022'd;
%let end_dt='27OCT2022'd;

data new;
    days = &new_date_field-&end_dt;



Paige Miller
Super User

You are not, repeat not, passing anything resembling a valid date in those shown values for the parameters Start_dt and End_dt.

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.

Super User Tom
Super User

But that is IS what your current code is doing.

You are passing the string '27OCT2022'd as the value of the new_date_field parameter of your %DATA_PULL() macro.



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.  If you needed to convert those strings into date values you would need to include an INPUT() function call in your code.  For example to calculate how many days from start to end you would have to use something like:

date_range=input(&end_dt,yymmdd10.) - input(&start_dt,yymmdd10.);

 Which will work because you included the quotes into the values of START_DT and END_DT .


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.


SAS does not care if you defined the macro to allow the values to be passed by position or not.  You can still pass them by name when you call the macro.  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.




Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg



Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1 like
  • 5 in conversation