Programming in SAS How SAS implements structured programming

  • Slides: 14
Download presentation
Programming in SAS How SAS implements structured programming constructs

Programming in SAS How SAS implements structured programming constructs

SAS Philosophy The “Elephant philosophy of programming” Remove everything that doesn’t look like an

SAS Philosophy The “Elephant philosophy of programming” Remove everything that doesn’t look like an elephant. SAS manipulates tables one row at a time.

DATA Steps l l l Program statements are executed within data steps Code is

DATA Steps l l l Program statements are executed within data steps Code is executed in the order that it appears Instruction sets in SAS end with a DATA, PROC, or RUN statement Each DATA step executes an implied loop from the first read statement (SET, MERGE, INPUT, etc. ) to the start of the next instruction set Data records are output at the end of the instruction set (implied OUTPUT) unless there is an OUTPUT statement

Sequence Statement 1 Statement 2 Statement 3 Statement 4 Statement …

Sequence Statement 1 Statement 2 Statement 3 Statement 4 Statement …

Loops Setup Statements DO WHILE Statement 1 Statement 2 Statement 3 Setup Statements Statement

Loops Setup Statements DO WHILE Statement 1 Statement 2 Statement 3 Setup Statements Statement 1 Statement 2 Statement 3 *** Statement n DO UNTIL

DO Statement DO; . . . more SAS statements. . . END; DO index-variable=specification-1

DO Statement DO; . . . more SAS statements. . . END; DO index-variable=specification-1 <, . . . specification-n>; . . . more SAS statements. . . END;

DO WHILE|UNTIL Statement l l The UNTIL expression is evaluated at the bottom of

DO WHILE|UNTIL Statement l l 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.

Examples n=0; do while(n<5); n= n+1; put n; end; n=0; do until (n<5); n=

Examples n=0; do while(n<5); n= n+1; put n; end; n=0; do until (n<5); n= n+1; put n; end;

Examples l l do i=1 to 10; do count=2 to 8 by 2; do

Examples l l 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);

Selection Select Case 1 Statement 1 a Statement 2 a Statement 3 a Statement

Selection Select Case 1 Statement 1 a Statement 2 a Statement 3 a Statement … Case 2 Statement 1 b Statement 2 b Statement 3 c Statement … Case 3 Statement 1 c Statement 2 c Statement 3 c Statement …

IF-THEN/ELSE Statement l IF expression THEN statement; <ELSE statement; > l l l Arguments

IF-THEN/ELSE Statement l IF expression THEN statement; <ELSE statement; > l l l Arguments expression • is any SAS expression and is a required argument. statement • can be any executable SAS statement or DO group.

SAS Selection Constructs l l IF … THEN …; ELSE …; IF … THEN

SAS Selection Constructs l l IF … THEN …; ELSE …; IF … THEN • DO; • • • END ELSE • DO; • • Statements • END; Other else statements.

SELECT Statement SELECT <(select-expression)>; WHEN-1 (when-expression-1 <. . . , when-expressionn>) statement; <. .

SELECT Statement SELECT <(select-expression)>; WHEN-1 (when-expression-1 <. . . , when-expressionn>) statement; <. . . WHEN-n (when-expression-1 <. . . , whenexpression-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.

SAS Selection Constructs (cont. ) SELECT (payclass); WHEN ('monthly') amt=salary; WHEN ('hourly') DO; amt=hrlywage*min(hrs,

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;