BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SWEETSAS
Obsidian | Level 7

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)

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
  • The macro is defined with DSN and SORTBY but then use SORTORDER not SORTBY in the macro call.
  • The %BQUOTE doesn't do what you think here, using double quotes is sufficient in this context
  • It also has dsnout versus dsnsout in the PROC PRINT

 

 

*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)


 

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

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; 

 

--
Paige Miller
Reeza
Super User
  • The macro is defined with DSN and SORTBY but then use SORTORDER not SORTBY in the macro call.
  • The %BQUOTE doesn't do what you think here, using double quotes is sufficient in this context
  • It also has dsnout versus dsnsout in the PROC PRINT

 

 

*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)


 

SWEETSAS
Obsidian | Level 7

Thanks @Reeza! This works! I was omitting quotation mark in the libname statement.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 1434 views
  • 2 likes
  • 4 in conversation