Monday, 17 March 2014

SAS_IfWhere_DoLoop

Q)How to include and exclude observations using subsetting IF.
include if
(expression) ;
exclude if not (expression) then delete;

Q)If Vs Where

·         The subsetting IF statement is executable; the WHERE statement is not.

·         The WHERE statement selects observations in SAS data sets only, whereas the subsetting IF statement selects observations from an existing SAS data set or from observations that are created with an INPUT statement.

·         The WHERE statement selects observations before they are brought into the program data vector, making it a more efficient programming technique. The subsetting IF statement works on observations after they are read into the program data vector.

·         The WHERE statement can produce a different data set from the subsetting IF when a BY statement accompanies a SET, MERGE, or UPDATE statement. The different data set occurs because SAS creates BY groups before the subsetting IF statement selects but after the WHERE statement selects.

·         The WHERE statement cannot be executed conditionally as part of an IF statement, but the subsetting IF statement can.

·         Do not confuse the WHERE statement with the DROP or KEEP statement. The DROP and KEEP statements select variables for processing. The WHERE statement selects observations.

Q)Consider the following SAS Program
data finance.earnings;
Amount=1000;
Rate=.075/12;
do month=1 to 12;
Earned+(amount+earned)*(rate);
end;
run;
What would be the value of month at the end of data step execution and how many observations would be there?
Answer: Value of month would be 13               No. of observations would be 1

Question: Consider the following SAS Program
data finance;
Amount=1000;
Rate=.075/12;
do month=1 to 12;
Earned+(amount+earned)*(rate);
output;
end;
run;
How many observations would be there at the end of data step execution?
Answer: 12

Question: How do you use the do loop if you don’t know how many times should you execute the do loop?
Answer: we can use do until or do while to specify the condition.

Question: What is the difference between do while and do until?
Answer:
An important difference between the DO UNTIL and DO WHILE statements is that the DO WHILE expression is evaluated at the top of the DO loop. If the expression is false the first time it is evaluated, then the DO loop never executes. Whereas DO UNTIL  executes at least once.

Question: How do you specify number of iterations and specific condition within a single do loop?
Answer:
data work;
do i=1 to 20 until(Sum>=20000);
Year+1;
Sum+2000;
Sum+Sum*.10;
end;
run;

This iterative DO statement enables you to execute the DO loop until Sum is greater than or equal to 20000 or until the DO loop executes 10 times, whichever occurs first

 

 

No comments:

Post a Comment