Presentation is loading. Please wait.

# 1 EPIB 698C Lecture 6 Raul Cruz-Cano. 2 Using IF-THEN statement IF-THEN statement is used for conditional processing. Example: you want to derive means.

## Presentation on theme: "1 EPIB 698C Lecture 6 Raul Cruz-Cano. 2 Using IF-THEN statement IF-THEN statement is used for conditional processing. Example: you want to derive means."— Presentation transcript:

1 EPIB 698C Lecture 6 Raul Cruz-Cano

2 Using IF-THEN statement IF-THEN statement is used for conditional processing. Example: you want to derive means test scores for female students but not male students. Here we derive means conditioning on gender =‘female’ Syntax: If condition then action; Eg: If gender =‘F’ then mean_score =mean(scr1, scr2);

3 Using IF-THEN statement Logical comparisonMnemonic termsymbol Equal toEQ= Not equal toNE^= or ~= Less thanLT< Less than or equal toLE<= Greater thanGT> greater than or equal toGE>= Equal to one in a listIN List of Logical comparison operators Note: Missing numeric values will be treated as the most negative values you can reference on your computer

4 Using IF-THEN statement Example: We have data contains the following information of subjects: Age Gender Midterm Quiz FinalExam 21 M 80 B- 82 20 F 90 A 93 35 M 87 B+ 85 48 F 80 C 76 59 F 95 A+ 97 15 M 88 C 93 Task: To group student based on their age ( =60)

data conditional; input Age Gender \$ Midterm Quiz \$ FinalExam; datalines; 21 M 80 B- 82 20 F 90 A 93 35 M 87 B+ 85 48 F 80 C 76 59 F 95 A+ 97 15 M 88 C 93 ; data new1; set conditional; if Age < 20 then AgeGroup = 1; if 20 <= Age < 40 then AgeGroup = 2; if 40 <= Age < 60 then AgeGroup = 3; if Age >= 60 then AgeGroup = 4; run; 5 CODE

6 Multiple conditions with AND and OR IF condition1 and condition2 then action; Eg: If age <40 and gender=‘F’ then group=1; If age <40 or gender=‘F’ then group=2;

7 IF-THEN statement, multiple conditions Example: We have data contains the following information of subjects: Age Gender Midterm Quiz FinalExam 21 M 80 B- 82 20 F 90 A 93 35 M 87 B+ 85 48 F 80 C 76 59 F 95 A+ 97 15 M 88 C 93 Task: To group student based on their age ( =40),and gender

8 data new1; set conditional; If age <40 and gender='F' then group=1; If age >=40 and gender='F' then group=2; IF age <40 and gender ='M' then group=3; IF age >=40 and gender ='M' then group=4; run; CODE

Note: Missing numeric values will be treated as the most negative values you can reference on your computer Example: group age into age groups with missing values 21 M 80 B- 82 20 F 90 A 93. M 87 B+ 85 48 F 80 C 76 59 F 95 A+ 97. M 88 C 93 9 CODE

10 Multiple actions with Do, end Syntax: IF condition then do; Action1 ; Action 2; End; If age <=20 then do ; group=1; exam_date =“Monday”; End;

11 IF-THEN statement, with multiple actions Example: We have data contains the following information of subjects: Age Gender Midterm Quiz FinalExam 21 M 80 B- 82 20 F 90 A 93 35 M 87 B+ 85 48 F 80 C 76 59 F 95 A+ 97 15 M 88 C 93 Task: To group student based on their age, and assign test date based on the age group CODE

12 IF-THEN/ELSE statement Syntax IF condition1 then action1; Else if condition2 then action2; Else if condition3 then action3; IF-THEN/Else statement has two advantages than IF-THEN statement (1) It is more efficient, use less computing time (2) Else logic ensures that your groups are mutually exclusive so that you do not put one observation into more than one groups.

13 IF-THEN/ELSE statement data new1; set conditional; if Age < 20 then AgeGroup = 1; else if Age >= 20 and Age < 40 then AgeGroup = 2; else if Age >= 40 and Age < 60 then AgeGroup = 3; else if Age >= 60 then AgeGroup = 4; run; DATA contest; INFILE 'C:\pumpkin.txt'; INPUT Name \$16. Age Type \$ @23 Date MMDDYY10. (Scr1 Scr2 Scr3 Scr4 Scr5) (4.1); If age < =10 then mean_score =mean(Scr1,Scr2); else mean_score=mean(Scr1,Scr2,Scr3,Scr4, Scr5); AvgScore= MEAN(Scr1,Scr2,Scr3,Scr4, Scr5); DayEntered = DAY(Date); Type = UPCASE(Type); run; CODE

14 The IN operator If you want to test if a value is one of the possible choices, you can use multiple “OR” statement like this: IF grade =‘A’ or grade =‘B’ or grade =‘C’ then PASS=‘yes’; A alternative is to use a IN operator: IF grade in (‘A’ ‘B’ ‘C’) then PASS=‘yes’; IF grade in (‘A’, ‘B’,‘C’) then PASS=‘yes’; CODE (error)

15 Simplifying programs with Arrays SAS Arrays are a collection of elements (usually SAS variables) that allow you to write SAS statements referencing this group of variables. Arrays are defined using Array statement as: ARRAY name (n) variable list name: is a name you give to the array n: is the number of variables in the array eg: ARRAY store (4) macys sears target costco Store(1) is the variable for macys Store(2) is the variable for sears

16 Simplifying programs with Arrays A radio station is conducting a survey asking people to rate 10 songs. The rating is on a scale of 1 to 5, with 1=Do not like the song; 5-like the song; IF the listener does not want to rate a song, he puts a “9” to indicate missing values Here is the data with location, listeners age and rating for 10 songs Albany 54 4 3 5 9 9 2 1 4 4 9 Richmond 33 5 2 4 3 9 2 9 3 3 3 Oakland 27 1 3 2 9 9 9 3 4 2 3 Richmond 41 4 3 5 5 5 2 9 4 5 5 Berkeley 18 3 4 9 1 4 9 3 9 3 2 We want to change 9 to missing values (.)

17 Simplifying programs with Arrays DATA songs; INFILE 'F:\radio.txt'; INPUT City \$ 1-15 Age domk wj hwow simbh kt aomm libm tr filp ttr; ARRAY song (10) domk wj hwow simbh kt aomm libm tr filp ttr; DO i = 1 TO 10; IF song(i) = 9 THEN song(i) =.; END; run; Repetitive Structures

18 Using shortcuts for lists of variable names When writing SAS programs, we will often need to write a list of variables names. When you have a data with many variables, a shortcut for lists of variables names is helpful Numbered range list: variables which starts with same characters and end with consecutive number can be part of a numbered range list Eg : INPUT cat8 cat9 cat10 cat11 INPUT cat8 – cat11

19 Using shortcuts for lists of variable names Name range list: name range list depends on the internal order, or position of the variables in a SAS dataset. This is determined by the appearance of the variables in the DATA step. Eg : Data new; Input x1 x2 y2 y3; Run; Then the internal range list is: x1 x2 y2 y3 Shortcut for this variable list is x1-y3; Proc contents procedure with the POSITION option can be used to find out the internal order

Download ppt "1 EPIB 698C Lecture 6 Raul Cruz-Cano. 2 Using IF-THEN statement IF-THEN statement is used for conditional processing. Example: you want to derive means."

Similar presentations

Ads by Google