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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
Tom
Super User Tom
Super User

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;

SP_SAS
Obsidian | Level 7

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

Tom
Super User Tom
Super User

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;

SP_SAS
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 5 replies
  • 4911 views
  • 3 likes
  • 2 in conversation