DATA Step, Macro, Functions and more

ERROR: Macro keyword DO appears as text

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 15
Accepted Solution

ERROR: Macro keyword DO appears as text

Any help in making the below program work would be great. I get error only when I put %do i = 1 %to 4; ,&&a&i  %end; in the sum function. I need to dynamically add values of some macro variables there.

%macro testa;

%let a1 = 2;
%let a2 = 0;
%let a3 = 0;
%let a4 = 0;


%do v = 1 %to 10;

  %do %while(1<2 ) ;

   %if %sysfunc(sum(0 %do i = 1 %to 4; ,&&a&i  %endSmiley Wink)<6 %then %do;
   data a&v;
   a=&v;
   run;
   %goto leave;
   %end; /*End of %if statement*/
   %else %do;
  
   data _null_;
   a=sleep(1,1);
   run;
   %end;

  %end; /*end of while*/
  %leave:


%end; /*end of v*/


%mend testa;


Accepted Solutions
Solution
‎10-14-2013 02:59 PM
Super User
Super User
Posts: 7,039

Re: ERROR: Macro keyword DO appears as text

If you are strictly using integers then you could replace %SYSEVALF() with %EVAL().


%let suma = 0;

%do i=1 %to 4 ;

  %let suma=%sysevalf(&suma + &&a&i) ;

%end;

%if %sysevalf(&suma < 6) %then %do;

View solution in original post


All Replies
Solution
‎10-14-2013 02:59 PM
Super User
Super User
Posts: 7,039

Re: ERROR: Macro keyword DO appears as text

If you are strictly using integers then you could replace %SYSEVALF() with %EVAL().


%let suma = 0;

%do i=1 %to 4 ;

  %let suma=%sysevalf(&suma + &&a&i) ;

%end;

%if %sysevalf(&suma < 6) %then %do;

Occasional Contributor
Posts: 15

Re: ERROR: Macro keyword DO appears as text

Thanks Tom, I created a work around using your first comment.

Super User
Super User
Posts: 7,039

Re: ERROR: Macro keyword DO appears as text

If your upper limit is hard coded then it is probably easier to just spell it out.

%if %sysfunc(sum(0,&a1,&a2,&a3,&a4))<6 %then %do;


or


%if ((0 + &a1 + &a2 + &a3 + &a4)<6) %then %do;

Occasional Contributor
Posts: 15

Re: ERROR: Macro keyword DO appears as text

Thanks a lot for your response.

My upper limit is hard coded but how many &a1...&an will be there that varies on each run and is determined dynamically.  Also if one of the &&a&i has null then using + will give me null that is why I wanted to use sum() function.

Super User
Super User
Posts: 7,039

Re: ERROR: Macro keyword DO appears as text

Depending on how you are creating them you might want to make sure that the macro variables have a period instead of empty string in that case.

For example this will be true it you create them from data step variables.

data _null_;

   a1=1; a2=2 ; a3=.; a4=.;

   call symputx('a1',a1);

   call symputx('a2',a2);

   call symputx('a3',a3);

   call symputx('a4',a4);

run;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 2736 views
  • 3 likes
  • 2 in conversation