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.

sas-innovate-2024.png

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.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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