DATA Step, Macro, Functions and more

Why a do loop index is great than a counter within do loop

Reply
Learner
Posts: 1

Why a do loop index is great than a counter within do loop

[ Edited ]

The following SAS program is submitted:

data WORK.LOOP;

X = 0;

do Index = 1 to 5 by 2;

X = Index;

end;

run;

 

how the answer is coming x=5 and Index=7? can anybody explain me what is happening step by step?

Contributor
Posts: 44

Re: SAS

Posted in reply to sukannya1

Hi 

  

Here  you we assigned X=0 and index=1  

it increments 1 ,3 ,5 and 7 . In index=5 condition is true then X is 5

and again do loop by 2 value is added (index=7) and condition is falls 

so X=5 and index=7  

 

 

 

Super User
Super User
Posts: 9,421

Re: SAS

Posted in reply to sukannya1

The loop is incremented at the end of the loop.  Hence when the end is hit index gets 2 added to it, but as that is greater than 5 it does not loop back to the do part.

 

Super User
Posts: 13,321

Re: Why a do loop index is great than a counter within do loop

Posted in reply to sukannya1

Perhaps if you look at the resulting data set done this way it will help.

This creates an output record for each time the loop executes.

data WORK.LOOP;
   X = 0;
  do Index = 1 to 5 by 2;
     X = Index;
      output;
   end;

run;
Ask a Question
Discussion stats
  • 3 replies
  • 146 views
  • 2 likes
  • 4 in conversation