DATA Step, Macro, Functions and more

My code doesn't want to work inside a DO loop- not accessing value?

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 260
Accepted Solution

My code doesn't want to work inside a DO loop- not accessing value?

 

Hey,

 

I have code like:

 

%let year = 15;

DATA my_new_file_&year;                           
   SET source_file&year;
calculate stuff;
make things;
run;

proc freq data = my_new_file_&year;
	tables variable;
	format;
	title "&year stuff";
run;

that works fine and is very useful

 

When i want to do 16 (or 17 or 18) I copy and past the block of code and make

%let year = 16;

or whatever year I want to run.

 

 What I want to do is something like

%macro my_yearly_stuff; 
%do myyear = 15 %to 18;

%let year = %myyear code block- all my working statements calling &year here %end; %mend my_yearly_stuff;

Run all the statement in a loop that iterates from 15 to 18 and executes the code block for each year.   But when I do that I get nothing.  No error.  No warning. Just nothing. The code is echoed in the log window but nothing happens.

 

What am i doing wrong?  How do i access the value of myyear and get it into the code?


Accepted Solutions
Solution
‎09-07-2017 11:16 AM
Super User
Super User
Posts: 7,844

Re: My code doesn't want to work inside a DO loop- not accessing value?

If it does absolutely nothing then perhaps it is because you never called it?

Also you are referencing an undefined macro where I think you want to reference an macro variable.

If your going to the trouble of making a macro you might as well use some parameters.

%macro my_yearly_stuff(startyr,endyr); 
%local year;
%do year = &startyr %to &endyr;

... code block- all my working statements calling &year here

%end; 
%mend my_yearly_stuff;

%my_yearly_stuff(15,18);

 

View solution in original post


All Replies
PROC Star
Posts: 8,092

Re: My code doesn't want to work inside a DO loop- not accessing value?

Change the %myyear to &myyear and make sure to end the statement with a semi-colon

 

Art, CEO, AnalystFinder.com

 

Super User
Posts: 22,818

Re: My code doesn't want to work inside a DO loop- not accessing value?

%macro my_yearly_stuff; 
%do myyear = 15 %to 18;
%let year = %myyear -> not needed, use the myYear macro variable directly. 
code block- all my working statements calling &myYear here

%end; 
%mend my_yearly_stuff;

Note the small changes needed. 

Solution
‎09-07-2017 11:16 AM
Super User
Super User
Posts: 7,844

Re: My code doesn't want to work inside a DO loop- not accessing value?

If it does absolutely nothing then perhaps it is because you never called it?

Also you are referencing an undefined macro where I think you want to reference an macro variable.

If your going to the trouble of making a macro you might as well use some parameters.

%macro my_yearly_stuff(startyr,endyr); 
%local year;
%do year = &startyr %to &endyr;

... code block- all my working statements calling &year here

%end; 
%mend my_yearly_stuff;

%my_yearly_stuff(15,18);

 

Super Contributor
Super Contributor
Posts: 260

Re: My code doesn't want to work inside a DO loop- not accessing value?

We have a winner.

 

I wasn't calling it.

 

I had

%mend my_yearly_stuff;

 

which is exactly what the example showed.

 

I changed that to

 

%mend;
%my_yearly_stuff;

 

and it runs. 

Super User
Posts: 12,994

Re: My code doesn't want to work inside a DO loop- not accessing value?

The code

%mend my_yearly_stuff;

 tells us two things, one it is the end of a macro definition and (if use properly) that the macro it is ending is my_yearly_stuff. If you have any text other than the macro it is ending you will get a warning in the log.

 

Better would be:

 

%mend my_yearly_stuff;

 

%my_yearly_stuff;

Frequent Contributor
Posts: 113

Re: My code doesn't want to work inside a DO loop- not accessing value?

%macro my_yearly_stuff;
%do myyear = 15 %to 18;
*%let year = %myyear; *Not required (IMO);
code block- all my working statements calling &myyear. here

%end;
%mend my_yearly_stuff;
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 120 views
  • 3 likes
  • 6 in conversation