BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Time_Looper47
Obsidian | Level 7

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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;

 


 

View solution in original post

6 REPLIES 6
Reeza
Super User
Macro variables are local within the macro and dont' exist once the macro is done. I would suggest doing this in a data step and using CALL SYMPUTX() which allows a third parameter that you can specify as scope, local or global.
novinosrin
Tourmaline | Level 20

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

Reeza
Super User

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;

 


 

Time_Looper47
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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;

 

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 913 views
  • 4 likes
  • 4 in conversation