Presentation on theme: "How SAS implements structured programming constructs"— Presentation transcript:
1 How SAS implements structured programming constructs Programming in SASHow SAS implements structured programming constructs
2 SAS Philosophy The “Elephant philosophy of programming” Remove everything that doesn’t look like an elephant.SAS manipulates tables one row at a time.
3 DATA Steps Program statements are executed within data steps Code is executed in the order that it appearsInstruction sets in SAS end with a DATA, PROC, or RUN statementEach DATA step executes an implied loop from the first read statement (SET, MERGE, INPUT, etc.) to the start of the next instruction setData records are output at the end of the instruction set (implied OUTPUT) unless there is an OUTPUT statement
5 Loops Setup Statements Setup Statements DO WHILE Statement 1 Statement n***DO UNTILStatement 1Statement 2Statement 3Statement n***
6 DO Statement DO; ...more SAS statements... END; DO index-variable=specification-1 <, specification-n>; more SAS statements . . .
7 DO WHILE|UNTIL Statement The UNTIL expression is evaluated at the bottom of the loop after the statements in the DO loop have been executed. If the expression is true, the DO loop does not iterate again.The WHILE expression is evaluated at the top of the loop before the statements in the DO loop are executed. If the expression is true, the DO loop iterates. If the expression is false the first time it is evaluated, the DO loop does not iterate even once.
8 Examplesn=0;do while(n<5);n= n+1;put n;end;do until (n<5);
9 Examples do i=1 to 10; do count=2 to 8 by 2; do i=1 to 10 while (x<y);do i=2 to 20 by 2 until((x/3)>y);
10 Selection Select Case 1 Case 2 Case 3 Statement 1a Statement 1b Statement 1cStatement 2aStatement 2bStatement 2cStatement 3aStatement 3cStatement 3cStatement …Statement …Statement …
11 IF-THEN/ELSE Statement IF expression THEN statement; <ELSE statement;>Argumentsexpressionis any SAS expression and is a required argument.statementcan be any executable SAS statement or DO group.
12 SAS Selection Constructs IF … THEN …; ELSE …;IF … THENDO;StatementsENDELSEEND;Other else statements.
13 SELECT StatementSELECT <(select-expression)>;WHEN-1 (when-expression-1 <..., when-expression-n>) statement;<... WHEN-n (when-expression-1 <..., when-expression-n>) statement;><OTHERWISE statement;END;(select-expression) specifies any SAS expression that evaluates to a single value.(when-expression) specifies any SAS expression, including a compound expression.
14 SAS Selection Constructs (cont.) SELECT (payclass);WHEN ('monthly') amt=salary;WHEN ('hourly')DO;amt=hrlywage*min(hrs,40);IF hrs>40 THEN PUT 'Check Timecard';END; /* end of do */OTHERWISE PUT 'Problem Observation';END;