BookmarkSubscribeRSS Feed
Sas_noob25
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;

run;

 

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);

run;

%mend;

%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!

 

 

5 REPLIES 5
mkeintz
PROC Star

Use DATA DATE literals, which are expressed either as

    '27oct2022'd

or

  "27oct2022"d.

 

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

 

Therefore:

 

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.

 

 

--------------------------
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

--------------------------
Sas_noob25
Obsidian | Level 7

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

PaigeMiller
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);
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;

 

 

--
Paige Miller
ballardw
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.

Tom
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.

%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.

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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