BookmarkSubscribeRSS Feed
sukannya1
Calcite | Level 5

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?

3 REPLIES 3
tlnarayana26
Calcite | Level 5

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  

 

 

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

 

ballardw
Super User

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;

sas-innovate-2024.png

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.

 

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