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:
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.
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!
DATA DATE literals, which are expressed either as
The latter honors macro values between the quotes, the earlier does not.
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',"&new_date_field"d,end_dt); put days=; run; %mend; options mprint; %data_pull(2022,'2022-10-27','2022-10-31',27OCT2022);
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.
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); run; %mend; %data_pull('31OCT2022'd,'27OCT2022'd)
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; run;
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.
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.
%data_pull (yyyy=2022 ,start_dt='2022-10-27' ,end_dt='2022-10-31' ,new_date_field='27OCT2022'd );
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.
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.