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

 

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

6 REPLIES 6
art297
Opal | Level 21

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

 

Art, CEO, AnalystFinder.com

 

Reeza
Super User
%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. 

Tom
Super User Tom
Super User

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);

 

HB
Barite | Level 11 HB
Barite | Level 11

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. 

ballardw
Super User

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;

ShiroAmada
Lapis Lazuli | Level 10
%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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 740 views
  • 3 likes
  • 6 in conversation