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

Dear SAS Experts,

 

I am trying to sum a series using a do loop but I cannot seem to figure out how to do that as I am getting errors in all my attempts. 

Untitled.png

here is what tried and failed:

do while(n<addyrs);
do t = 6 to (n+6);
sum = 0;
sum = EMLP/((1+K)**t);
end;
end;

when I run this code I get the following error:

ERROR: Invalid DO loop control information, either the INITIAL or TO expression
is missing or the BY expression is missing, zero, or invalid.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @AmirSari,

 

Simplify your code and use a single DO loop:

data want(drop=t);
K=0.02; EMLP=17; Addyrs=42; /* replace with your values */
do t=6 to 6+Addyrs;
  sum + EMLP/((1+K)**t);
end;
run;

Or use the formula for the geometric sum, then you don't need a loop at all:

data want(drop=q);
K=0.02; EMLP=17; Addyrs=42; /* replace with your values */
q=1/(1+K);
sum=EMLP*q**6*(1-q**(Addyrs+1))/(1-q);
run;

View solution in original post

5 REPLIES 5
gamotte
Rhodochrosite | Level 12

Hello,

Can you show a complete runnable code ?

Here, you will have an infinite loop as n is not modified inside the loop.

The error message can result from variables in the do statements having missing values.

gamotte
Rhodochrosite | Level 12

Also, in your code, sum is reinitialized at each step.

I think you meant :

 

sum=0; n=0;
do while(n<addyrs);
do t = 6 to (n+6);
      sum + EMLP/((1+K)**t);
   end;
n+1; end;

Edit : added an n increment  to avoid an infinite loop but it might not be what you need.

FreelanceReinh
Jade | Level 19

Hello @AmirSari,

 

Simplify your code and use a single DO loop:

data want(drop=t);
K=0.02; EMLP=17; Addyrs=42; /* replace with your values */
do t=6 to 6+Addyrs;
  sum + EMLP/((1+K)**t);
end;
run;

Or use the formula for the geometric sum, then you don't need a loop at all:

data want(drop=q);
K=0.02; EMLP=17; Addyrs=42; /* replace with your values */
q=1/(1+K);
sum=EMLP*q**6*(1-q**(Addyrs+1))/(1-q);
run;
ballardw
Super User

It is almost never a waste of time to provide a small example data set, especially in the form of data step code, of the start and the desired result.

AmirSari
Quartz | Level 8
Thanks everybody for your replies! I will provide an example next time!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1353 views
  • 2 likes
  • 4 in conversation