Hello All,
I have the below program:
%let myDT = '23JAN2022'D; %macro dts(mnth_1=); data want; test_dt=&mnth_1.; format test_dt date9.; run; %put @@@&myDT; %put @@@&mnth_1; %mend; %dts(mnth_1=intnx('month', &myDT., -1, 's'));
Initially, I have the macro variable
&myDT.
which I pass it to the macro
%dts(mnth_1=intnx('month', &myDT., -1, 's'))
in order to find last month's date.
Even though I am able to use the
&mnth_1.
in order to create the dataset, when I use the
%PUT
statement to write the value it doesn't work.
Any ideas please?
To print the value of a VARIABLE use the PUT statement. The %PUT statement is a MACRO STATEMENT and so will know nothing about any variables you might have created in your data step.
%macro dts(mnth_1=);
%put &=mnth_1;
data want;
test_dt=&mnth_1.;
format test_dt date9.;
put test_dt = ;
run;
%mend;
Let's try it:
2106 %let myDT = '23JAN2022'D; 2107 %dts(mnth_1=intnx('month', &myDT., -1, 's')); MNTH_1=intnx('month', '23JAN2022'D, -1, 's') MPRINT(DTS): data want; MPRINT(DTS): test_dt=intnx('month', '23JAN2022'D, -1, 's'); MPRINT(DTS): format test_dt date9.; MPRINT(DTS): put test_dt = ; MPRINT(DTS): run; test_dt=23DEC2021 NOTE: The data set WORK.WANT has 1 observations and 1 variables.
As a first step, please define what the purpose of the macro should be.
I'm going to avoid the macro %dts and just create a macro variable named &dts.
%let dts=%nrstr(mnth_1=intnx('month', &myDT., -1, 's'));
%put &=dts;
Then you can use &DTS anywhere you want. As to why you want this, I still don't understand, and I get the feeling you have over-complicated things.
You could also do this to create macro variable &MNTH_1
%let mnth1 = %sysfunc(intnx(month,&mydt,-1,s));
So, please explain in much more detail what you want, why you want it, and even better ... give us an example of what you are doing without macros and without macro variables. As I said, I think you have greatly over-complicated things.
mnth_1 is a macro parameter, not a macro. What is the purpose of the macro dts?
To print the value of a VARIABLE use the PUT statement. The %PUT statement is a MACRO STATEMENT and so will know nothing about any variables you might have created in your data step.
%macro dts(mnth_1=);
%put &=mnth_1;
data want;
test_dt=&mnth_1.;
format test_dt date9.;
put test_dt = ;
run;
%mend;
Let's try it:
2106 %let myDT = '23JAN2022'D; 2107 %dts(mnth_1=intnx('month', &myDT., -1, 's')); MNTH_1=intnx('month', '23JAN2022'D, -1, 's') MPRINT(DTS): data want; MPRINT(DTS): test_dt=intnx('month', '23JAN2022'D, -1, 's'); MPRINT(DTS): format test_dt date9.; MPRINT(DTS): put test_dt = ; MPRINT(DTS): run; test_dt=23DEC2021 NOTE: The data set WORK.WANT has 1 observations and 1 variables.
I don't see the need to use a macro at all. Why can't you use
mnth_1 = intnx('month', &myDT., -1, 's');
in a data step?
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.