notes9 - Conditional Execution Compute revenue figures...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Conditional Execution Compute revenue figures based on flight destination. DESTINATION CLASS AIRFARE LAX First 2000 Economy 1200 DFW First 1500 Economy 900 data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if Dest='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if Dest='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; 1 4 Notes 9: Conditional Execution Selecting Rows Conditional Execution International Airlines wants to compute revenue for Los Angeles and Dallas flights based on the prices in the table below. DESTINATION CLASS LAX First Economy DFW First Economy AIRFARE 2000 1200 1500 900 Conditional Execution data flightrev; TRUE set st.dfwlax; Total=sum(FirstClass,Economy); if Dest='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if Dest='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (First Observation) Flight Date 439 2 Dest First Economy Total Revenue Class 14955 LAX 20 137 157 . 5 Conditional Execution General form of IF-THEN and ELSE statements: IF expression THEN statement; IF expression THEN statement; ELSE statement; ELSE statement; Expression contains operands and operators that form a set of instructions that produce a value. Operands are variable names constants. Operators are symbols that request a comparison a logical operation an arithmetic calculation SAS functions. Only one executable statement is allowed on an IF-THEN or ELSE statement. Conditional Execution data flightrev; TRUE set st.dfwlax; Total=sum(FirstClass,Economy); if Dest='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if Dest='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (First Observation) Flight Date 439 3 Dest First Economy Total Revenue Class 14955 LAX 20 137 157 204400 6 1 Conditional Execution data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if Dest='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if Dest='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (First Observation) Flight Date 439 Dest First Economy Total Revenue Class 14955 LAX 20 137 157 204400 Conditional Execution data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if Dest='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if Dest='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (Fourth Observation) Flight Date 982 Dest First Economy Total Revenue Class 14956 dfw 5 85 90 . 7 10 Conditional Execution data flightrev; FALSE set st.dfwlax; Total=sum(FirstClass,Economy); if Dest='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if Dest='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (Fourth Observation) Flight Date 982 Dest First Economy Total Revenue Class 14956 dfw 5 85 90 . Conditional Execution proc print data=flightrev; format Date date9.; run; The SAS System First Class 20 20 15 5 14 15 17 7 . 14 Obs 1 2 3 4 5 6 7 8 9 10 Flight 439 921 114 982 439 982 431 982 114 982 Date 11DEC2000 11DEC2000 12DEC2000 12DEC2000 13DEC2000 13DEC2000 14DEC2000 14DEC2000 15DEC2000 15DEC2000 Dest LAX DFW LAX dfw LAX DFW LaX DFW LAX DFW Economy 137 131 170 85 196 116 166 88 187 31 Total 157 151 185 90 210 131 183 95 187 45 Revenue 204400 147900 234000 . 263200 126900 . 89700 224400 48900 11 8 Why are two Revenue values missing? Conditional Execution data flightrev; FALSE set st.dfwlax; Total=sum(FirstClass,Economy); if Dest='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if Dest='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (Fourth Observation) Flight Date 982 Dest First Economy Total Revenue Class 14956 dfw 5 85 90 . The UPCASE Function You can use the UPCASE function to convert letters from lowercase to uppercase. General form of the UPCASE function: UPCASE (argument) UPCASE (argument) 9 12 2 Conditional Execution Use the UPCASE function to convert the Dest values to uppercase for the comparison. data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if upcase(Dest)='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if upcase(Dest)='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; Conditional Execution TRUE data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if upcase(Dest)='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if upcase(Dest)='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (Fourth Observation) Flight Date 982 upcase('dfw')='DFW' Dest First Economy Total Revenue Class 14956 dfw 5 85 90 84000 13 16 Conditional Execution FALSE data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if upcase(Dest)='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if upcase(Dest)='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (Fourth Observation) Flight Date 982 Conditional Execution proc print data=flightrev(obs=5); format Date date9.; run; The SAS System First Class 20 20 15 5 14 Obs 1 2 3 4 5 Flight 439 921 114 982 439 Date 11DEC2000 11DEC2000 12DEC2000 12DEC2000 13DEC2000 Dest LAX DFW LAX dfw LAX Economy 137 131 170 85 196 Total 157 151 185 90 210 Revenue 204400 147900 234000 84000 263200 upcase('dfw')='DFW' Dest First Economy Total Revenue Class 14956 dfw 5 85 90 . The OBS= data set option specifies when to stop processing. 14 17 Conditional Execution TRUE data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if upcase(Dest)='LAX' then Revenue=sum(2000*FirstClass,1200*Economy); else if upcase(Dest)='DFW' then Revenue=sum(1500*FirstClass,900*Economy); run; PDV (Fourth Observation) Flight Date 982 Conditional Execution You can use the DO and END statements to execute a group of statements based on a condition. General form of the DO and END statements: IF expression THEN DO; IF expression THEN DO; executable statements executable statements END; END; ELSE DO; ELSE DO; executable statements executable statements END; END; 18 upcase('dfw')='DFW' Dest First Economy Total Revenue Class 14956 dfw 5 85 90 . 15 3 Conditional Execution Use DO and END statements to execute a group of statements based on a condition. data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; end; run; 19 The LENGTH Statement You can use the LENGTH statement to define the length of a variable explicitly. General form of the LENGTH statement: LENGTH variable(s) $ length; LENGTH variable(s) $ length; Example: length City $ 11; 22 Conditional Execution proc print data=flightrev; var Dest City Flight Date Revenue; format Date date9.; run; The SAS System Obs 1 2 3 4 5 6 7 8 9 10 Dest LAX DFW LAX dfw LAX DFW LaX DFW LAX DFW City Los An Dallas Los An Dallas Los An Dallas Los An Dallas Los An Dallas Flight 439 921 114 982 439 982 431 982 114 982 Date 11DEC2000 11DEC2000 12DEC2000 12DEC2000 13DEC2000 13DEC2000 14DEC2000 14DEC2000 15DEC2000 15DEC2000 Revenue 204400 147900 234000 84000 263200 126900 233200 89700 224400 48900 20 The LENGTH Statement data flightrev; set st.dfwlax; length City $ 11; Total=sum(FirstClass,Economy); if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; end; run; Why are City values truncated? 23 Variable Lengths At compile time, the length of a variable is determined the first time the variable is encountered. data flightrev; set st.dfwlax; Total=sum(FirstClass,Economy); if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; 6 characters end; between the quotes: run; Length=6 21 The LENGTH Statement proc print data=flightrev; var Dest City Flight Date Revenue; format Date date9.; run; The SAS System Obs 1 2 3 4 5 6 7 8 9 10 Dest LAX DFW LAX dfw LAX DFW LaX DFW LAX DFW City Los Angeles Dallas Los Angeles Dallas Los Angeles Dallas Los Angeles Dallas Los Angeles Dallas Flight 439 921 114 982 439 982 431 982 114 982 Date 11DEC2000 11DEC2000 12DEC2000 12DEC2000 13DEC2000 13DEC2000 14DEC2000 14DEC2000 15DEC2000 15DEC2000 Revenue 204400 147900 234000 84000 263200 126900 233200 89700 224400 48900 24 4 Subsetting Rows In a DATA step, you can subset the rows (observations) in a SAS data set with a Deleting Rows proc print data=over175; var Dest City Flight Date Total Revenue; format Date date9.; run; The SAS System Obs 1 2 3 4 Dest LAX LAX LaX LAX City Los Los Los Los Angeles Angeles Angeles Angeles Flight 114 439 431 114 Date 12DEC2000 13DEC2000 14DEC2000 15DEC2000 Total 185 210 183 187 Revenue 234000 263200 233200 224400 WHERE statement DELETE statement subsetting IF statement. The WHERE statement in a DATA step is the same as the WHERE statement you saw in a PROC step. 25 28 Deleting Rows You can use a DELETE statement to control which rows are written to the SAS data set. General form of the DELETE statement: IF expression THEN DELETE; IF expression THEN DELETE; Selecting Rows You can use a subsetting IF statement to control which rows are written to the SAS data set. General form of the subsetting IF statement: IF expression; IF expression; The expression can be any SAS expression. The subsetting IF statement is valid only in a DATA step. 26 29 The expression can be any SAS expression. Deleting Rows Delete rows that have a Total value that is less than or equal to 175. data over175; set st.dfwlax; length City $ 11; Total=sum(FirstClass,Economy); if Total le 175 then delete; if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; end; run; 27 Process Flow of a Subsetting IF DATA Statement Read Observation or Record IF expression False True Continue Processing Observation Output Observation to SAS Data Set 30 5 Selecting Rows Select rows that have a Total value that is greater than 175. Using SAS Date Constants The constant 'ddMMMyyyy'd (example: '14dec2000'd) creates a SAS date value from the date enclosed in quotes. data over175; set st.dfwlax; length City $ 11; Total=sum(FirstClass,Economy); if Total gt 175; if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; end; run; 31 dd is a one- or two-digit value for the day. MMM is a three-letter abbreviation for the month (JAN, FEB, MAR, and so on). yyyy d is a two- or four-digit value for the year. is required to convert the quoted string to a SAS date. 34 Selecting Rows proc print data=over175; var Dest City Flight Date Total Revenue; format Date date9.; run; The SAS System Obs 1 2 3 4 Dest LAX LAX LaX LAX City Los Los Los Los Angeles Angeles Angeles Angeles Flight 114 439 431 114 Date 12DEC2000 13DEC2000 14DEC2000 15DEC2000 Total 185 210 183 187 Revenue 234000 263200 233200 224400 Using SAS Date Constants data over175; set st.dfwlax; length City $ 11; Total=sum(FirstClass,Economy); if Total gt 175 and Date lt '14dec2000'd; if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; end; run; 32 35 Selecting Rows The variable Date in the st.dfwlax data set contains SAS date values (numeric values). 01JAN1960 store 0 display 01/01/1959 01/01/1960 12/14/2000 366 ??? Obs 1 2 Using SAS Date Constants proc print data=over175; var Dest City Flight Date Total Revenue; format Date date9.; run; The SAS System Dest LAX LAX City Los Angeles Los Angeles Flight 114 439 Date 12DEC2000 13DEC2000 Total 185 210 Revenue 234000 263200 01JAN1961 14DEC2000 What if you only wanted flights that were before a specific date, such as 14DEC2000 14DEC2000? 33 36 6 Subsetting Data What if the data were in a raw data file instead of a SAS data set? data over175; infile 'raw-data-file'; input @1 Flight $3. @4 Date mmddyy8. @12 Dest $3. @15 FirstClass 3. @18 Economy 3.; length City $ 11; Total=sum(FirstClass,Economy); if Total gt 175 and Date lt '14dec2000'd; if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; end; 37 run; WHERE or Subsetting IF? Use a WHERE statement and a subsetting IF statement in the same step. data over175; set st.dfwlax; where Date lt '14dec2000'd; length City $ 11; Total=sum(FirstClass,Economy); if Total gt 175; if upcase(Dest)='DFW' then do; Revenue=sum(1500*FirstClass,900*Economy); City='Dallas'; end; else if upcase(Dest)='LAX' then do; Revenue=sum(2000*FirstClass,1200*Economy); City='Los Angeles'; end; run; 40 Subsetting Data proc print data=over175; var Dest City Flight Date Total Revenue; format Date date9.; run; The SAS System Obs Obs 1 2 Dest LAX LAX City Los Angeles Los Angeles Flight 114 439 Date 12DEC2000 13DEC2000 Total 185 210 Revenue 234000 263200 1 2 WHERE or Subsetting IF? proc print data=over175; var Dest City Flight Date Total Revenue; format Date date9.; run; The SAS System Dest LAX LAX City Los Angeles Los Angeles Flight 114 439 Date 12DEC2000 13DEC2000 Total 185 210 Revenue 234000 263200 38 41 WHERE or Subsetting IF? Step and Usage PROC step DATA step (source of variable) INPUT statement Assignment statement SET statement (single data set) SET/MERGE (multiple data sets) Variable in ALL data sets Variable not in ALL data sets Yes No Yes Yes No No Yes Yes Yes Yes WHERE Yes IF No Lab Exercises 8 Creating Variables Using Conditional Execution Selecting Rows 39 42 7 ...
View Full Document

This note was uploaded on 10/31/2011 for the course STAT 440 taught by Professor Muyot,m during the Fall '08 term at University of Illinois, Urbana Champaign.

Ask a homework question - tutors are online