I want to use %LET statement to create a macro variable that holds a directory name. And then use the macro variable created in the %LET statement in a LIBNAME statement inside a macro. This is giving me error. See example below. Thanks for your help in advance.
%let dir=C:\data\check;
%macro rpt(dsn=, sortby=);
libname mylib %BQUOTE(%STR(&dir));
proc sort data=mylib.&dsn out=dsnout;
by &sortby;
run;
proc print data=dsnsout;
run;
%mend rpt;
%rpt(dsn=motor,sortorder=cars)
*create fake data to test;
libname mylib "/home/fkhurshed/Demo1/";
data mylib.cars;
set sashelp.cars;
run;
libname mylib;
*actual work;
*macro debugging options;
options mprint symbolgen;
%let dir=/home/fkhurshed/Demo1/;
%macro rpt(dsn=, sortby=);
libname mylib "&dir";
proc sort data=mylib.&dsn out=dsnout;
by &sortby;
run;
proc print data=dsnout (obs=10);
run;
%mend rpt;
%rpt(dsn=cars, sortby=origin)
@SWEETSAS wrote:
I want to use %LET statement to create a macro variable that holds a directory name. And then use the macro variable created in the %LET statement in a LIBNAME statement inside a macro. This is giving me error. See example below. Thanks for your help in advance.
%let dir=C:\data\check;
%macro rpt(dsn=, sortby=);
libname mylib %BQUOTE(%STR(&dir));
proc sort data=mylib.&dsn out=dsnout;
by &sortby;
run;proc print data=dsnsout;
run;
%mend rpt;
%rpt(dsn=motor,sortorder=cars)
Why so complicated?
%let dir=C:\data\check;
libname mylib "&dir.";
will do it.
First, when executing the macro, you must specify the same arguments as when the macro is defined. So when executing the macro, you give an argument named SORTORDER, which does not exist when you defined the macro.
Secondly, macros and macro variables create SAS code when the program runs. The code create by the macro or macro variable must be legal valid working SAS code. If it is not legal valid working SAS code, then the macro will not run. This macro creates the SAS code
libname mylib C:\data\check;
which is incorrect. What do you need to do to the above line to fix it and make it work? (Please answer without referring to macro variables or macros, please)
Lastly, when debugging macro, run this command before you execute the macro.
options mprint;
For even more detailed debugging, use
options mprint mlogic symbolgen;
*create fake data to test;
libname mylib "/home/fkhurshed/Demo1/";
data mylib.cars;
set sashelp.cars;
run;
libname mylib;
*actual work;
*macro debugging options;
options mprint symbolgen;
%let dir=/home/fkhurshed/Demo1/;
%macro rpt(dsn=, sortby=);
libname mylib "&dir";
proc sort data=mylib.&dsn out=dsnout;
by &sortby;
run;
proc print data=dsnout (obs=10);
run;
%mend rpt;
%rpt(dsn=cars, sortby=origin)
@SWEETSAS wrote:
I want to use %LET statement to create a macro variable that holds a directory name. And then use the macro variable created in the %LET statement in a LIBNAME statement inside a macro. This is giving me error. See example below. Thanks for your help in advance.
%let dir=C:\data\check;
%macro rpt(dsn=, sortby=);
libname mylib %BQUOTE(%STR(&dir));
proc sort data=mylib.&dsn out=dsnout;
by &sortby;
run;proc print data=dsnsout;
run;
%mend rpt;
%rpt(dsn=motor,sortorder=cars)
Thanks @Reeza! This works! I was omitting quotation mark in the libname statement.
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 16. 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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: