user650738
user650738

Reputation: 31

How do I invoke a macro variable inside the quoted string of a libname statement in SAS

I have a libname that varies from year to year and I wanted to make a program that automatically adjusts for this. But in order for everything to work I have to have invoke a macro inside of the quoted string in a libname statement. How do I do this?

%macro srvyr;

data work.whatever;

length srvyr $4.;

srvyr = (left(year(date()))-1);


srvyr2 = "'C:\Documents and Settings\user\Desktop\sas\d"||srvyr||"a1'";


run;

%mend;

%srvyr;  

/*Everything above sets configures the pathname the way I need it*/

I want to then run this:

libname stuff &srvyr;run;

as if it were

libname stuff 'C:\Documents and Settings\user\Desktop\sas\d2010a1';
run;

How do I do this right?

Upvotes: 3

Views: 10868

Answers (2)

Laurent de Walick
Laurent de Walick

Reputation: 2174

Does is always have to be the previous year, or do you want to base it on a value in a dataset. You don't need macro to solve this.

The shortest method to get last year is as follows

libname stuff "C:\Documents and Settings\user\Desktop\sas\d%eval(%sysfunc(year(%sysfunc(date())))-1)a1";

and if you want to break it up to make it more readable it could be like this

%let lastyear = %eval(%sysfunc(year(%sysfunc(date())))-1);
%let libpath = C:\Documents and Settings\user\Desktop\sas\d&lastyear.a1;

libname stuff "&libpath";

Upvotes: 4

Hong Ooi
Hong Ooi

Reputation: 57696

call symput is your friend. Put the following inside the data step, after creating the variable srvyr2:

call symput('srvyr_path', srvyr2);

and then outside the macro,

libname stuff &srvyr_path;

Upvotes: 2

Related Questions