DATA Step, Macro, Functions and more

%scan has too many arguments

Posts: 25

%scan has too many arguments

Hi Guys, I am stuck with this piece of code. Basically the problem is my macro variable has commas and I am not sure how to get it resolved. can someone help e up with this???µ

%let nm_list=Assistance#Credit and suretyship#Fire and other damage#Motor vehicle liability#Motor, other



%LET i = 1;

%DO %WHILE (%NRBQUOTE(%SCAN(&nm_list,&i,#)) NE %QUOTE());

%LET nm = %SCAN(&nm_list,&i,#);

%LET k = 1;

%DO %WHILE (%NRBQUOTE(%SCAN(&ccy_list,&k,#)) NE %QUOTE());

%LET ccy = %SCAN(&ccy_list,&k,#);

DATA Report_TP_E4_Final;

SET tp_e4_sec1;

WHERE nm = "&nm"

AND ccy_code = "&ccy";


proc report data=Report_TP_E4_Final;


%LET k = %EVAL(&k + 1);


%LET i = %EVAL(&i + 1);




The only problem is with Motor,List otherwise this works fine. Any help is really appreciated

classes#Worker compensation;

Occasional Contributor
Posts: 8

%scan has too many arguments

I could not make your code work but try with %quote

%let nm_list=%QUOTE(Assistance#Credit and suretyship#Fire and other damage#Motor vehicle liability#Motor, other ) ;

Posts: 7,363

%scan has too many arguments

Worked for me, as did using %bquote.  You didn't provide the %let statement for ccy_list, so I just used the same one as you provided for nm_list, but enclosed with a %bquote.

Regular Contributor
Posts: 241

Re: %scan has too many arguments

I am partial to separating out the part that is repeated from the part that loops. This makes it simple to test the repeated part. I am also for making the macro arguments formal and declaring all local macro variables explicitly. One good side effect of using the formal arguments is that this makes it clear who is responsible for quoting what.

For instance, if the macro caller does not quote the comma that is within the names parameter value, then even the macro invocation will fail; the macro author cannot do anything about it; thus the quoting responsibility naturally falls on to the macro user. Within the macro, however, the macro author should have used %qscan function, instead of %scan, when certain characters are expected, because %scan function returns an unquoted text. hth

   %macro report(name, ccy);
      %put ***&name***&ccy***;
   %mend  report;

   %macro e5(names=, ccys=, dlm=#);
      %if %superq(names) = %then %return;
      %if %superq(ccys) = %then %return;

      %local i j name ccy;

      %let i = 1;
      %let name = %qscan(&names, &i, &dlm);
      %do %while (&name ^=);
         %let j = 1;
         %let ccy = %qscan(&ccys, &j, &dlm);
         %do %while (&ccy ^=);

            %report(&name, &ccy)

            %let j = %eval(&j + 1);
            %let ccy = %qscan(&ccys, &j, &dlm);
         %let i = %eval(&i + 1);
         %let name = %qscan(&names, &i, &dlm);
   %mend  e5;

   %let names = a#b, b#c;
   %let ccys = 1#2#3;
   %e5(names=%superq(names), ccys=&ccys)
   %*-- on log
   ***b, b***1***
   ***b, b***2***
   ***b, b***3***

Super User
Posts: 9,681

Re: %scan has too many arguments

data class;
 set sashelp.class;
 if name='Mary' then name='M,ar y';run;

data have;
input name &$10. age ;
Philip   14
Ronald   15
M,ar y   16
proc sql;
 create table want as 
  select * from have(keep=name),have(keep=age);quit;
%macro report(name,age);
proc report data=class nowd;
where name="&name" and age=&age;
%mend report;
data _null_;
 set want;
 call execute('%report(%str('||name||'),%str('||age||'));');


Ask a Question
Discussion stats
  • 4 replies
  • 5 in conversation