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?
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);
Change the %myyear to &myyear and make sure to end the statement with a semi-colon
Art, CEO, AnalystFinder.com
%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.
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);
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.
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;
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.
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.