Grouping Statements Using DO Groups

The Basics of Using Grouping Statements and DO Groups

You can execute a group of statements as a unit by using DO groups.
To construct a DO group, you use the DO and END statements along with other SAS statements.
Syntax, DO group:
DO;
SAS statements
END;
  • The DO statement begins DO-group processing.
  • SAS statements between the DO and END statements are called a DO group and are executed as a unit.
  • The END statement terminates DO-group processing.
Tip:You can nest DO statements within DO groups.
You can use DO groups in IF-THEN/ELSE statements and SELECT groups to execute many statements as part of the conditional action.

Examples: DO and END Statements

In this simple DO group, the statements between DO and END are performed only when TotalTime is greater than 800. If TotalTime is less than or equal to 800, statements in the DO group are not executed, and the program continues with the assignment statement that follows the appropriate ELSE statement.
data clinic.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45; 
   TotalTime=(timemin*60)+timesec; 
   retain SumSec 5400; 
   sumsec+totaltime; 
   length TestLength $ 6 Message $ 20; 
   if totaltime>800 then  
      do;
         testlength='Long';
         message='Run blood panel';
      end; 
   else if 750<=totaltime<=800 then testlength='Normal'; 
   else if totaltime<750 then TestLength='Short'; 
run;
In the SELECT group below, the statements between DO and END are performed only when the value of Payclass is hourly. Notice that an IF-THEN statement appears in the DO group; the PUT statement executes only when Hours is greater than 40. The second END statement in the program closes the SELECT group.
data payroll; 
   set salaries; 
   select(payclass); 
   when ('monthly') amt=salary; 
   when ('hourly')
      do;
         amt=hrlywage*min(hrs,40);
         if hrs>40 then put 'CHECK TIMECARD';
      end; 
   otherwise put 'PROBLEM OBSERVATION'; 
   end; 
run;

Indenting and Nesting DO Groups

You can nest DO groups to any level, just like you nest IF-THEN/ELSE statements.
Note: The memory capabilities of your system might limit the number of nested DO statements that you can use.
Here is an example of nested DO groups:
do;
	statements;
		do;
			statements;
				do;
					statements;
				end;
		end;
end;
Tip
It is good practice to indent the statements in DO groups, as shown in the preceding statements, so that their position indicates the levels of nesting.
Note: There are three other forms of the DO statement:
  • The iterative DO statement executes statements between DO and END statements repetitively based on the value of an index variable. The iterative DO statement can contain a WHILE or UNTIL clause.
  • The DO UNTIL statement executes statements in a DO loop repetitively until a condition is true, checking the condition after each iteration of the DO loop.
  • The DO WHILE statement executes statements in a DO loop repetitively while a condition is true, checking the condition before each iteration of the DO loop.
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.233