Hello, I'm attempting to do a do loop within a macro to create month variables as you can see, however it is telling me the "symbolic reference is not resolved" when I try to %put the values to the log.
%macro my_month_loop;
data _null_;
%do i=1 %to 12;
%let month_&i = %unquote(%str(%')%sysfunc(intnx(month,%sysfunc(today()),-&i,beginning),yymmddd10.)%str(%'));
%end;
run;
%mend my_month_loop;
%put &month_1;
%put &month_2;
%put &month_3;
%put &month_4;
%put &month_5;
%put &month_6;
%put &month_7;
%put &month_8;
%put &month_9;
%put &month_10;
%put &month_11;
%put &month_12;
Personally, I'd use a data _null_ step instead.
EDIT: modified to include quotes, since your original had them included.
data _null_;
start_date=today();
do i=1 to 12;
date=intnx('month', start_date, i, 'b');
call symputx(catx("_", 'month', i), quote(put(date, yymmddd10.), "'"), 'g');
end;
run;
%put &month_1.;
%put &month_12.;
@Time_Looper47 wrote:
Hello, I'm attempting to do a do loop within a macro to create month variables as you can see, however it is telling me the "symbolic reference is not resolved" when I try to %put the values to the log.
%macro my_month_loop; data _null_; %do i=1 %to 12; %let month_&i = %unquote(%str(%')%sysfunc(intnx(month,%sysfunc(today()),-&i,beginning),yymmddd10.)%str(%')); %end; run; %mend my_month_loop; %put &month_1; %put &month_2; %put &month_3; %put &month_4; %put &month_5; %put &month_6; %put &month_7; %put &month_8; %put &month_9; %put &month_10; %put &month_11; %put &month_12;
When put statements attempt to resolve a local macrovariable list, the put statements should be placed within a macro definition. So try executing the put statements inside the macro. Also, you do not need data step compiler/datastep to process macro statements
Personally, I'd use a data _null_ step instead.
EDIT: modified to include quotes, since your original had them included.
data _null_;
start_date=today();
do i=1 to 12;
date=intnx('month', start_date, i, 'b');
call symputx(catx("_", 'month', i), quote(put(date, yymmddd10.), "'"), 'g');
end;
run;
%put &month_1.;
%put &month_12.;
@Time_Looper47 wrote:
Hello, I'm attempting to do a do loop within a macro to create month variables as you can see, however it is telling me the "symbolic reference is not resolved" when I try to %put the values to the log.
%macro my_month_loop; data _null_; %do i=1 %to 12; %let month_&i = %unquote(%str(%')%sysfunc(intnx(month,%sysfunc(today()),-&i,beginning),yymmddd10.)%str(%')); %end; run; %mend my_month_loop; %put &month_1; %put &month_2; %put &month_3; %put &month_4; %put &month_5; %put &month_6; %put &month_7; %put &month_8; %put &month_9; %put &month_10; %put &month_11; %put &month_12;
Interesting. What if I wanted to start at todays month, and move backwards 12 months - I changed it to -i instead of i to accomplish going backwards however I want it to start with todays month, and then start going back. As if the loop started at i=0 nd then i=-1 and then i=-2, and so on. So I want the value of month_1 to be '2019-10-01'. I tried changing the start of the loop to i=0 to 11 but it didn't work.
Why not loop from 0 to -11 by -1?
Or make a truth table of the values of the macro variable suffix and the offset number you want to use and figure out if there is mathematical formula to map one to the other.
Suffix Offset
1 0
2 -1
3 -2
...
12 -11
So looks like OFFSET is 1-SUFFIX.
do suffix=1 to 12;
call symputx(cats('month_',suffix)
,cats("'",put(intnx('month',today(0),1-suffix),yymmdd10.),"'")
);
end;
Two problems. First you never ran the macro. Second you didn't make the macro variables global (or create them before running the macro) so they are gone after the macro finishes. Also there is one bit on total non-sense. Why do you have a DATA step wrapped around the %DO loop in your macro definition?
%macro my_month_loop;
%local i;
%do i=1 %to 12;
%global month_&i;
%let month_&i = %unquote(%str(%')%sysfunc(intnx(month,%sysfunc(today()),-&i,b),yymmddd10.)%str(%'));
%end;
%mend my_month_loop;
%my_month_loop;
%put &month_1;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.