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.
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.