Hi everyone,
I'm trying to make myself use macros more and have come up with an error in a basic macro that will assign a libname by year and file location. See below:
%macro yearc;
%let year= 2012 2013 2014 2015;
%do i=1 %to 4;
%let yearold=%scan(&year, &i);
libname old&yearold '\\cdc.gov\project\CCID_NCIRD_DBD_MVPDB\Epi Team\Pertussis Epi\EIP\Enhanced Surveillance Project\Data\Formatted Data\Historic Datasets\Static_1999_&yearold_Dataset';
%end;
%mend yearc;
%yearc;
The macro revolves the first &yearold varible in the libname statement creating the libarary, but it doesn't work with the macro variable in the libname statement for the file pathway. It just resolves to C:\\My Documents\Old_Datasets\Finalized_1999_&yearold._Dataset. Am I missing something? Any suggestions would be much appreciated!
Thanks!
Macro variables do not resolve inside single quotes.
Use
libname old&yearold "\\cdc.gov\project\CCID_NCIRD_DBD_MVPDB\Epi Team\Pertussis Epi\EIP\Enhanced Surveillance Project\Data\Formatted Data\Historic Datasets\Static_1999_&yearold._Dataset";
Also when using a macro variable in a compound you need to have the dot operator to tell SAS to concatenate the following text.
&yearold_dataset would be considered the whole macro variable name.
See what result you get with:
%put &yearold_dataset;
to get familiar with the error message.
Also look at this and see that the scan, at least in your specific example is not needed.
%macro dummy; %do YearOld = 2012 %to 2014; %put libname old&yearold; %end; %mend dummy; %dummy;
Macro triggers (% and &) are not resolved when enclosed in single quotes. Use double quotes instead.
Macro variables do not resolve inside single quotes.
Use
libname old&yearold "\\cdc.gov\project\CCID_NCIRD_DBD_MVPDB\Epi Team\Pertussis Epi\EIP\Enhanced Surveillance Project\Data\Formatted Data\Historic Datasets\Static_1999_&yearold._Dataset";
Also when using a macro variable in a compound you need to have the dot operator to tell SAS to concatenate the following text.
&yearold_dataset would be considered the whole macro variable name.
See what result you get with:
%put &yearold_dataset;
to get familiar with the error message.
Also look at this and see that the scan, at least in your specific example is not needed.
%macro dummy; %do YearOld = 2012 %to 2014; %put libname old&yearold; %end; %mend dummy; %dummy;
Hi,
Thank you both so much for your help! I guess I tried replacing the double-quotes and adding a period, but not at the same time. I will put this in my memory bank for the next macro I do. I'm slowly learning!
Thanks again,
cc1986
A very helpful diagnostic with macros are the system options MPRINT SYMBOLGEN and MLOGIC.
They show the generated code, the resolution of macro variables and the results of macro logic (%if and similar) statements.
Options mprint symbolgen; /*turn on the option */
%macrocall
Options nomprint nosymbolgen; /* to turn off*/
Will provide a bit more information in the log to diagnose many issues.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.