BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tianerhu
Pyrite | Level 9
libname results 'C:\practice everyday\output';
data results.output13_2;
total = 100;
do year = 1 to 3;
  do month = 1 to 12;
   total + total*0.05;
 output;
 end;
 total + 50;
end;
run;
proc print data = results.output13_2;
run;

tianerhu_0-1621304576456.png

libname results 'C:\practice everyday\output';
data results.output13_2;
total = 100;
do year = 1 to 3;
do month = 1 to 12;
total + total*0.05;
*output;
end;
total + 50;
end;
run;
proc print data = results.output13_2;
run;

 

tianerhu_1-1621304604122.png

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

In your first program you have an explicit OUTPUT statement inside the DO loop.  Therefore the normal implicit OUTPUT (just before the RUN; statement) is eliminated.  But in your second program, there is no explicit OUTPUT, so the implicit OUTPUT is executed.

 

So the question really is "why does the iterator YEAR in DO YEAR=1 to 3; ... END; end up as a 4"?   The iterator is always incremented (from 1 to 2, 2 to 3, 3 to 4) at the end of each iteration..  Once YEAR=4 there is no subsequent iteration of the DO loop executed, because it is now outside of the upper limit of 3.  Here's code to show that:

 

data _null_;
  do year=1 to 3 ;
    put 'inside ' year=;
  end;
  put 'after  ' year=;
run;

 

Now, if you really want to iterate from year=1 to 3 without year being incremented to 4 you can add an UNTIL expression:

data _null_;
  do year=1 to 3 until (year=3);
    put 'inside ' year=;
  end;
  put 'after  ' year=;
run;    

This works because the UNTIL expression (telling the loop when to stop) is evaluated at the end of each iteration, but before the iterator is incremented.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

View solution in original post

5 REPLIES 5
mkeintz
PROC Star

In your first program you have an explicit OUTPUT statement inside the DO loop.  Therefore the normal implicit OUTPUT (just before the RUN; statement) is eliminated.  But in your second program, there is no explicit OUTPUT, so the implicit OUTPUT is executed.

 

So the question really is "why does the iterator YEAR in DO YEAR=1 to 3; ... END; end up as a 4"?   The iterator is always incremented (from 1 to 2, 2 to 3, 3 to 4) at the end of each iteration..  Once YEAR=4 there is no subsequent iteration of the DO loop executed, because it is now outside of the upper limit of 3.  Here's code to show that:

 

data _null_;
  do year=1 to 3 ;
    put 'inside ' year=;
  end;
  put 'after  ' year=;
run;

 

Now, if you really want to iterate from year=1 to 3 without year being incremented to 4 you can add an UNTIL expression:

data _null_;
  do year=1 to 3 until (year=3);
    put 'inside ' year=;
  end;
  put 'after  ' year=;
run;    

This works because the UNTIL expression (telling the loop when to stop) is evaluated at the end of each iteration, but before the iterator is incremented.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
tianerhu
Pyrite | Level 9

Could you explain why the variable month become 13, the same way to the variable year ?

PaigeMiller
Diamond | Level 26

@tianerhu wrote:

Could you explain why the variable month become 13, the same way to the variable year ?


It's the same reason as year; the loop increments month to 13, then it stops as the loop will not run when month is 13.

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 448 views
  • 1 like
  • 3 in conversation