본문 바로가기

자격증/SAS BASE

Chapter 11 . Processing Data with Do Loops

Q. Which statement is false regarding the use of DO Loops?

  • They can be used to combine DATA ane PROC steps.    ( X )

- DO loops are DATA step statements and cannot be used in conjunction with PROC steps.

- Dp loops 는 DATA step statements 이며 PROC step 과 결합하여 사용할 수 없다.

 

 

[ 옳은 보기 ]

  • They can contain conditional clause.   ( O )
  • Then can generate multiple observation.  ( O )
  • They can be used to read data.  ( O )

 

 

 

Q. During each execution of the following DO loop, the value of Earned is calculated and is added to its previous value. How many times does this DO loop execute?   (12)

 

      data work.earnings;

      Amount = 1000;

      Rate = .75 / 12;

      do month = 1 to 12;          ------------------→  DO statment's stop value

         Earned + (amount + earned) * rate ;

      end;

 

  • The number of iterations is determined by the DO statment's stop value, which in this case is 12.
  • iterations 의 수는 DO 문의 stop value에 의해서 결정된다.

 

 

 

 

Q. On january 1 of each year, $5,000 is invested in an account. Complete the DATA step below to determine the value of the account after 15 years is a constant interest rate of 10% is expected.

 

      data work.invest;

      ...

           Capital + 5000;

           capital + (capital * .10);

      end;

      run;

 

 

  • do count= 1 to 15;

 

 

 

 

Q. In the data set Work.Invest,  what would be the stored value for Year?   ★  (2005)

 

      data work.invest;

         do year = 1990 to 2004;

              Capital + 5000;

              capital + (capital * .10) ;

        end;

      run; 

 

  • At the end of the 15th iteration of the DO loop, the value for Year is incremented to 2005.
  • Because this value exceeds the stop value, the DO loop ends.
  • At the bottom of the DATA step, the current values are written to the data set.

 

  • DO 문의 15번째 iteration 마지막에서, Year 변수의 값이 2005 로 증가한다.
  • DATA step 의 끝에서, Year 변수의 현재값이 data set 에 써진다.
  • Year 변수에 2005 가 저장됨을 주의 !!!

 

 

 

 

Q. Which of the following statements is false regarding the program shown below?

- OUTPUT statement -   

 

      data work.invest;

         do year=1990 to 2004 ;

             capital + 5000;

             capital + (capital*.10);

             output;        ----------------→ OUTPUT statement

         end;

      run;

 

 

  • The last value for Year in the new data set is 2005  ( X )
  • The OUTPUT statement overrides the automatic output at the end of the DATA step.
  • On the last iteration of the DO loop, the value of Year (2004) is written to the data set.

 

 

  •  OUTPUT 구문은 SAS 데이터 세트에 현재 처리 중인 관측치를 작성한다.
  • OUTPUT 구문은 데이터 스텝의 종료 시점에 작성되지 않고, OUTPUT 구문이 실행되는 즉시 데이터 세트에 현재 관측치를 작성한다.
  • 만약 OUTPUT 구문에 출력되는 데이터 세트 이름을 지정하지 않으면, 관측치는 DATA 구문에 나열된 모든 데이터 세트에 작성된다.
  • 출처 : https://statwith.tistory.com/3245
 

[SAS Statement] OUTPUT 구문

[SAS Statement] SAS 데이터 스텝 구문 사전 목록 o OUTPUT 구문 1. 목 적 OUTPUT 구문은 SAS 데이터 세트에 현재 처리 중인 관측치를 작성한다. OUTPUT 구문은 데이터 스텝의 종료 시점에 작성되지 않고, OUTPUT

statwith.tistory.com

 

 

[ 옳은 보기 ]

  • The OUTPUT statement writes current values to the data set immediately.
  • the OUTPUT statement overrides the automatic output at the end of the DATA step.
  • The DO loop performs 15 iterations.

 

 

 

 

Q. How many observation will the data set Work.Earn contain?   (20)

- OUTPUT statement -

      

      data work.earn;

      value=2000;

         do year=1 to 20;

             Interest=value*.075;

             value + interest;

             output;        ----------------→ OUTPUT statement

         end;

      run;

 

  • The number of observations is based on the number of times of OUTPUT statement executes.
  • The new data set has 20 observations, one for each iteration of the DO loop.

 

  • 관측값의 개수는 OUTPUT 의 시행횟수와 같다.

 

 

 

 

Q. Which of the following would you use to compare the result of investing $4,000 a year for five years, in three different banks that compound interests monthly? Assume a fixed rate for the five-year period.     ( nested DO loops.)

 

  • place the montly calculation in a DO loop within a DO loop that iterates once for each year.
  • DO WHILE and DO UNTIL statements are not used here because the number of required iteration is fixed.
  • A non-iterarive DO group would not be useful.
  • DO WHILE 과 DO UNTIL 문은 iterations 이 고정된 상황에서는 사용하지 않는다 !! 

 

[ 틀린 보기 ]

  • DO WHILE statement    
  • DO UNTIL statement    
  • a DO group

 

 

 

 

Q. Which statement is false regarding DO UNTIL statements?

- DO UNTIL -

  • The condition is evaluated at the top of the loop, before the enclosed statements are executed.  ( X )
  • The DO UNTIL condition is evaluated at the bottom of the loop, 
  • So, the enclosed statements are always executed at least once.

 

  • DO UNTIL 문의 조건은 loop의 하단에서 평가된다 !!

 

[ 옳은 보기 ]

  • The enclosed statements are always executed at least once.
  • SAS statements in the DO loop are executed until the specified condition is true.
  • The DO loop must have a closing END statement.

 

 

 

Q. Select the DO WHILE statement that would generate the same result as the program delow.

- DO WHILE - 

 

      data work.invest ;

          capital = 100000;

          do until  (Capital  gt  500000);       -----------------→  gt : Greater Than

              Year +1;

              capital + (capital * .10);

          end;

      run;

 

  • do while  (capital  le 500000) ;      --------------  le : Less than or Equal to

 

  • DO WHILE loop is evaluated at the top of the loop.
  • you specify the condition that must exist in order to execute the enclosed statements.

 

  • DO WHILE loop 는 loop 의 상단에서 평가된다 !!

 

 

 

 

Q. In the following program, complete the statement so that the program stops generateing observations when Distance reaches 250 miles or when 10 gallons of fuel have been used

 

      data work.go250;

           set cert.cars;

           do gallons 1 to 10    ...   ;

               Distance=gallons * mpg;

               output; 

           end;

     run;

 

 

  • while (distance <= 250) ;
  • The WHILE expression causes the DO loop to stop executing when the value of Distance become equal or greater than 250
  • WHILE expression 은  Distance 가 250 과 같아지거나 250 보다 커지면 Do loop 를 멈춘다.