Managing Data.doc - Topic Managing Data 1 Conditionally...

Info icon This preview shows pages 1–5. Sign up to view the full content.

Topic : Managing Data 1. Conditionally IF-THEN-ELSE execute SAS statements 2. SORT observations in a SAS data set 3. KEEP, DROP, DELETE, RENAME, RETAIN, BY, FILE, PUT and OUTPUT Statements 4. PROC FORMAT 5. SAS DO-LOOPS 6. SAS Array 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

1. Conditionally IF-THEN-ELSE execute SAS statements Conditional execution of data step program statements is implemented using the IF/THEN/ELSE statements. Syntax: IF expression THEN statement1 ; <ELSE statement2 ;> Observe that IF/THEN and ELSE are two separate SAS statements. Each time the IF statement is executed the expression following the IF is evaluated. When the expression is true for the observation, the statement following the THEN is executed. The ELSE statement, which is optional, can be used to control a specific action if the IF condition is false. Or try to fully understand the following statements: The inputs to the IF /ELSE statements are expression is an expression that is evaluated for being true or false. statement1 is a statement executed when expression is true. statement2 is a statement executed when expression is false. These examples show different ways of specifying the IF-THEN /ELSE statement. /*Example: IF-THEN*/ data if_01_a; input code @@; cards ; 1 2 3 ; data if_01_b; length type $ 8. ; set if_01_a; if code= 1 then type = 'Fix' ; if code= 2 then type = 'Variable' ; if code ^= 1 and code ^= 2 then type = 'Unknown' ; label type = 'Types of Mortgage Rate' ; run ; /*Example: IF-THEN/ELSE*/ data if_01_c; length type $ 8. ; set if_01_a; if code= 1 then type = 'Fix' ; else if code= 2 then type = 'Variable' ; else if code ^= 1 and code ^= 2 then type = 'Unknown' ; label type = 'Types of Mortgage Rate' ; run ; Let us see how to divide age group? data age_grp; input pat_id age @@; cards ; 2
Image of page 2
290156 66 299871 68 280256 64 270456 60 262156 58 263256 55 266456 53 250656 44 251256 43 257456 47 258356 48 244606 42 249456 41 233256 33 237656 37 228356 28 222606 22 219856 21 ; run ; data age_grp2; length agegrp $ 5. ; set age_grp; if 20 <= age < 35 then agegrp = '20-34' ; else if 35 <= age < 50 then agegrp = '35-49' ; else if 50 <= age < 65 then agegrp = '50-64' ; else if 65 <= age then agegrp = '65+' ; run ; proc freq ; tables agegrp/ list ; *tables agegrp/out = age_grp3; run ; Advice: A better way to write multiple IF statement is to use an ELSE before all but the first IF. The other IF-THEN/ELSE statements would look like this: If then; Else if then; Else if then; …………..; The effect of the ELSE statements is that when any IF statement is true, all the following ELSE statements are skipped. The advantage is to reduce computer time (since all the IF do not have to be tested) and evenly to avoid the following type of error. Would you see what will happen with the statements below? data er; input x @@ ; cards ; 1 2 3 4 5 ; run ; data er_2; set er; if x= 1 then x= 5 ; if x= 2 then x= 4 ; if x= 4 then x= 2 ; if x= 5 then x= 1 ; run ; data er_3; set er; if x= 1 then x= 5 ; else if x= 2 then x= 4 ; else if x= 4 then x= 2 ; else if x= 5 then x= 1 ; run ; /*Example: IF-THEN/DO*/ data oss_test; length course $ 8. school $ 4. ; 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

input course school score year; cards ; English NT 92 1998 English NT 94 1999 English NT 96 2000 English NT 91 2001 English NSS 88 1998 English NSS 80 1999 English NSS 84 2000 English NSS 82 2001 Math NT 86 1998 Math NT 88 1999 Math NT 89 2000 Math NT 86 2001 Math NT 90 2002 Math NSS 84 1998 Math NSS 88 1999 Math NSS 88 2000 Math NSS 92 2001 Math NSS 89 2002 ; data
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.
  • Fall '17
  • Array

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern