BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
JKim8069
Calcite | Level 5
11.4 In the data set Work.Invest, what would be the stored value for Year?
data work.invest;
do year=1990 to 2004;
Capital+5000;
capital+(capital*.10);
end;
run;
a. missing
b. 1990
c. 2004
d. 2005
Correct answer: d
At the end of the 15th iteration of the DO loop, the value for Year is incremented to 2005

Dear guys, can I know why it’s not 2004?
1 ACCEPTED SOLUTION

Accepted Solutions
mkeintz
PROC Star

The index variable (YEAR in your example) is incremented by 1 at the end of each iteration.  The looping will not stop until the index variable is outside of the range (1990 through 2004 in your example).

 

So, in the last iteration YEAR=2004, a valid value.  At the end of that iteration, it is incremented to 2005, at which point the index variable first becomes outside of range., and no more loops are executed.

--------------------------
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

1 REPLY 1
mkeintz
PROC Star

The index variable (YEAR in your example) is incremented by 1 at the end of each iteration.  The looping will not stop until the index variable is outside of the range (1990 through 2004 in your example).

 

So, in the last iteration YEAR=2004, a valid value.  At the end of that iteration, it is incremented to 2005, at which point the index variable first becomes outside of range., and no more loops are executed.

--------------------------
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

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

sas-innovate-white.png

Missed SAS Innovate in Orlando?

Catch the best of SAS Innovate 2025 — anytime, anywhere. Stream powerful keynotes, real-world demos, and game-changing insights from the world’s leading data and AI minds.

 

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 1 reply
  • 906 views
  • 1 like
  • 2 in conversation