DATA Step, Macro, Functions and more

Basic Libname Macro Issue

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Basic Libname Macro Issue

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!

 

 


Accepted Solutions
Solution
‎03-22-2017 12:49 PM
Super User
Posts: 11,343

Re: Basic Libname Macro Issue

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


All Replies
Super User
Posts: 7,854

Re: Basic Libname Macro Issue

Macro triggers (% and &) are not resolved when enclosed in single quotes. Use double quotes instead.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎03-22-2017 12:49 PM
Super User
Posts: 11,343

Re: Basic Libname Macro Issue

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;
Occasional Contributor
Posts: 5

Re: Basic Libname Macro Issue

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

Super User
Posts: 11,343

Re: Basic Libname Macro Issue

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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