When the output statement is specified in the SAS Do loop, why does the stored value for index variable differ?

1.8k Views Asked by At
  1. 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

The correct ans given in the SAS base prep guide is d.2005.

  1. In the below question the stored value is given as 2004. Which of the following statements is false regarding the program shown below?

    data work.invest; do year=1990 to 2004; capital+5000; capital+(capital*.10); output; end; run;

    • a. The OUTPUT statement writes current values to the data set immediately.
    • b. The stored value for Year is 2005.
    • c. The OUTPUT statement overrides the automatic output at the end of the DATA step.
    • d. The DO loop performs 15 iterations.

The correct answer for this is b.2005. (identify the false statement is the question).

3

There are 3 best solutions below

0
On BEST ANSWER

Key concepts:

  • The counter increments at the END of the loop.
  • The OUTPUT statement overrides implicit output at end of the data step and writes the current status of all variables to the data set.

In the first question, that means at the end of the loop, it increments to 2005, goes to the DO statement and says STOP because the DO condition is not met. At the end of the data step the output is generated with the counter at Year=2005.

In the second question because the OUTPUT statement is before the end of the counter so the value output is Year=2004. Additionally the output statement will generate a row for each value of Year, such that the 'stored value' isn't a good question because there will be rows of Year=1990 to 2004 in the output data set.

0
On

As explained in SAS manual http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000194540.htm

Once you use an OUTPUT statement to write an observation to any one data set, however, there is no implicit OUTPUT statement at the end of the DATA step

Therefore, year = 2005 in your first example, and max(year)=2004 in your second example.

0
On

you first code,

data work.invest;
    do year=1990 to 2004;
        Capital+5000;
        capital+(capital*.10);
    end;
run;

How it works

  1. The variable "year" will get incremented every time after the execution of loop statements are over

  2. During the final step i.e., when year=2004, the two statements inside the do loop will get executed and now year value will get incremented to year=2005. however this value 2005 will not satisfy the loop condition. hence execution of do loop is over.

  3. SAS now reaches the end of data step(There are no other statements to get executed). Now IMPLICIT output statement will get executed.

  4. We have two variables here "year" and "Capital". Both of these variables with latest values will be written to the data set

latest value for year is 2005.

Result:

year Capital

2005 174748.6493

now you second code,

data work.invest;
    do year=1990 to 2004;
        Capital+5000;
        capital+(capital*.10);
        output;
    end;
run;

Point to note: If there is an EXPLICIT output statement given, SAS will not execute the IMPLICIT output.

How it works

  1. The variable "year" will get incremented every time after the execution of loop statements are over

  2. After the second line "capital+(capital*.10);" IMPLICIT output statement will get executed. hence we will get 15 observations

  3. During the final step i.e., when year=2004, the two statements inside the do loop +output statement will get executed and now year value will get incremented to year=2005. however this value 2005 will not satisfy the loop condition. hence execution of do loop is over.

  4. Since there is an EXPLICIT output statement, SAS wont do anymore output(IMPLICIT)

Result:

year Capital

1990 5500

.....

2003 153862.4085

2004 174748.6493