I am creating a macro variable of the form:
Data _NULL_;
ThisDate=Date();
call symput("FirstDayTwoMonthBack",(IntNX("Month", ThisDate, -2, 'beginning')));
Run;
Then when you display the value of this macro variable it is just a "number", i would like to store it as a date format of the type: "2021-11-29" which means year 2021, month = 1, and day = 29.
Any advice on how to do this?
As others already mentioned I would keep it as a number. I would also use CALL SYMPUTX to avoid notes about type conversion.
The functions of the DATA Step can also be used in the SAS macro language using %SYSFUNC. Have a look at the below code sample.
data _null_;
ThisDate=Date();
call symputx("FirstDayTwoMonthBack", IntNX("Month", ThisDate, -2, 'beginning'));
run;
%put NOTE: &FirstDayTwoMonthBack;
%put NOTE: %sysfunc(putn(&FirstDayTwoMonthBack, yymmddd10.));
%let newDate = %sysfunc(intnx(month, %sysfunc(today()), -2, b));
%put NOTE: &=newDate;
usually, it is unnecessary to format a macro variable. the un-formatted macro variable works fine in all logical and arithmetical situations.
The only time you need to format a macro variable is when you are going to use it in a title, label or file name.
Data _NULL_;
ThisDate=Date();
call symput("FirstDayTwoMonthBack", put(IntNX("Month", ThisDate, -2, 'beginning'), yymmdd10.));
Run;
%put &FirstDayTwoMonthBack; 2021-09-01
I agree with @PaigeMiller that this isn't totally necessary, but that gives you what you want I think.
Please remember, @SasStatistics , that if you format the macro variable value, and you want to use it for arithmetic or logical purposes, then you will have to un-format it. That's two operations (formatting followed by un-formatting, and don't forget to do both, and don't get one of the operations wrong) instead of zero operations. Either will work if done properly, it's your choice.
As others already mentioned I would keep it as a number. I would also use CALL SYMPUTX to avoid notes about type conversion.
The functions of the DATA Step can also be used in the SAS macro language using %SYSFUNC. Have a look at the below code sample.
data _null_;
ThisDate=Date();
call symputx("FirstDayTwoMonthBack", IntNX("Month", ThisDate, -2, 'beginning'));
run;
%put NOTE: &FirstDayTwoMonthBack;
%put NOTE: %sysfunc(putn(&FirstDayTwoMonthBack, yymmddd10.));
%let newDate = %sysfunc(intnx(month, %sysfunc(today()), -2, b));
%put NOTE: &=newDate;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.