macros

Reply
Contributor
Posts: 62

macros

%macro print(libname,dslist=a b c);
%do i=1 %to 4;
proc print data=&libname..%scan(&dslist,&i);
run;
%end;
%mend;

%print(pawan,dslist=test p1 test1 test2);

 

here if i place quotes in (dslist="test p1 test1 test2") it's not working..........in which scenerio have to place quotes and not.

 

%macro print1(libname,dslist="pawan test test1");
%do i=1 %to 4;
data _null_;
call symput('a',scan(&dslist,&i));
run;
proc print data=&libname..&a;
run;
%end;
%mend;

here it is not working without quotes?

Super User
Posts: 9,854

Re: macros

Then put double quote into separator list and remain the validate variable name .

 

 

%macro print(libname,dslist=a b c);
%do i=1 %to 4;
 %put &libname..%scan(&dslist,&i,,kdf);
%end;
%mend;


%print(pawan,dslist=test p1 test1 test2)

%print(pawan,dslist="test p1 test1 test2")
Super User
Posts: 5,352

Re: macros

Always, always always ... when you use macro language you have to picture what the program is going to look like.

 

While KSharp gave you a correct answer, here is some of the reasoning behind it.  Picture the result of:

 

dslist="test pi test1 test2"

 

When %scan operates on this string, the first word in the list is "test and the last word is test2".  If you pictured that, you would expect to get an error for coding data=&libname.."test;

 

KSharp correctly noted that you can add doublequotes to your list of delimeters that %can uses, so that the first word woul be test instead of "test.

 

When you switch to a DATA step, the SCAN function expects to see a character string as the first parameter.  So the first word within "test pi test1 test2" would be test and not "test.  

 

I expect that you have a solution for your problem now, as well as what you need to do to avoid future problems.

Ask a Question
Discussion stats
  • 2 replies
  • 279 views
  • 0 likes
  • 3 in conversation