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 %end;))<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;
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;
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;
Thanks Tom, I created a work around using your first comment.
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;
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.
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.