BookmarkSubscribeRSS Feed
Flottemesch
Calcite | Level 5

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

 

 

 

 

 

3 REPLIES 3
Quentin
Super User

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";
BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

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.

ballardw
Super User

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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 916 views
  • 0 likes
  • 4 in conversation