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

Dear SAS users,

 

I have a basic question. Why is i 5.5 upon the following code when the output statement is not given:

 

data A;
do i = 1 to 5 by 0.5;
   y = i**2; 
   output;
end;
run;
proc print data = A;
run;

data A;
do i = 1 to 5 by 0.5;
   y = i**2; 
end;
run;
proc print data = A;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

@vstorme wrote:

I can see that, but when you do add output as in the first case, i=5 in the last observation


That's because the implicit

i + 0.5;

comes after the execution of the output statement, and no implicit output is done after the do loop finishes.

Do this for reference:

data A;
do i = 1 to 5 by 0.5;
   y = i**2; 
   output;
  put i=;
end;
put i=;
run;

View solution in original post

5 REPLIES 5
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Read the manual!

i is incremented at the end of the loop to 5.5, however as that is over the top boundary for the do loop, the do loop then exits.  The incremenetor will always (upon complete run) be one iteration count above the upper bound.  Its a bit like saying:

set i to lower limit
start
is i over upper limit, no then do
  ...
increment i by one iteration and return to start
s_lassen
Meteorite | Level 14

I suppose you mean your last data step. The thing is this: if there is no explicit OUTPUT statement in a data step, SAS puts an implicit output statement at the end of the data step. After the loop has run, I=5.5, and that value is output. But for Y you will get 4.5**2, as that was the last assignment inside the loop.

vstorme
Obsidian | Level 7

I can see that, but when you do add output as in the first case, i=5 in the last observation

Kurt_Bremser
Super User

@vstorme wrote:

I can see that, but when you do add output as in the first case, i=5 in the last observation


That's because the implicit

i + 0.5;

comes after the execution of the output statement, and no implicit output is done after the do loop finishes.

Do this for reference:

data A;
do i = 1 to 5 by 0.5;
   y = i**2; 
   output;
  put i=;
end;
put i=;
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 855 views
  • 2 likes
  • 4 in conversation