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

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!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

4 REPLIES 4
ballardw
Super User

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;
cc1986
Calcite | Level 5

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

ballardw
Super User

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2699 views
  • 0 likes
  • 3 in conversation