I have a question regarding library mapping and Macro variables. The situation is that I would like to dynamically map libraries within a Macro based upon user inputs. The error I am encountering is that when I attempt to combine two Macro variables, I receive an error from the libref function.
Here is a short-code example:
/*declare test macro variables*/
%let st_yr = 1999;
%let loc = y:\data\
%let loc2 = "y:\data\1999
/*test string manipulation*/
%let test = %str(%"&loc.&st_yr.%");
%put &loc2.;
%put &test.;
/*NOTE: loc2 and test should be equivalent strings*/
/*map libraries*/
libname test1 &loc2.; /*No error and maps library*/
libname test2 &test..;
/*ERROR: "Libref in LIBNAME statement must be followed either by quoted string or engine name or semicolon; """ found.
Does anyone have advice on what is happening. I thought the two periods after the &test in the libname state would resolve the issue, but it doesn't.
regards
Hi,
Sometimes when you use macro quoting functions, SAS does not automatically unquote them as it should. In this case, you can often use the %UNQUOTE function to force the macro quoting symbols to be removed, e.g.:
%let st_yr = 1999;
%let loc = y:\data\ ;
%let test = %str(%"&loc.&st_yr.%");
libname test2 %unquote(&test);
Note in this case since you don't have unmatched quotes, you shouldn't need the macro quoting at all, so you could code it more simply as:
%let st_yr = 1999;
%let loc = y:\data\ ;
%let test = "&loc&st_yr";
libname test2 &test;
Or:
%let st_yr = 1999;
%let loc = y:\data\ ;
libname test2 "&loc&st_yr";
Avoid macro quoting if you can. In particular for something like this where you need to generated a quoted string to use to generate part of a SAS command try using the QUOTE() function to generate the quoted string. That way you can both properly handle and embedded quotes but also add single qutoes on the outside so any macro triggers that might be part of the actual file name or path will be protected.
%let st_yr = 1999;
%let loc = y:\data\ ;
%let loc2 = y:\data\1999 ;
%let test = &loc.&st_yr.;
libname test1 %sysfunc(quote(&loc2),%str(%'));
libname test2 %sysfunc(quote(&test),%str(%'));
SAS also has a macro %TSLIT() that adds single quotes.
Pleas ensure that the code examples you post are the ones you actually tested.
From what I see in your code:
/*declare test macro variables*/ %let st_yr = 1999; %let loc = y:\data\ <= missing semicolon %let loc2 = "y:\data\1999 <= missing close " and semicolon
I would expect anything using either &loc or &loc2 to misbehave in some fashion.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.