SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Reading Hierarchical Files
Creating One Observation per Detail Record


Conditionally Executing SAS Statements

You can use the value of type to identify each record. If type is H, you need to execute an INPUT statement to read the values for Address. However, if type is P then execute an INPUT statement to read the values for Name, Age, and Gender.


     data perm.people;
        infile census;
        retain Address;
        input type $1. @;    
V---+----10---+----
H 321 S. MAIN ST 
P MARY E    21 F
P WILLIAM M 23 M 
P SUSAN K    3 F


You can then direct SAS software to perform a given task based on a specific condition using an IF-THEN statement.


General form, IF-THEN statement:
IF expression THEN statement;
     <ELSE statement>;

where expression is any valid SAS expression and statement is any executable SAS statement.


IF-THEN statements are used to define conditions and actions. If a condition (defined by a SAS expression) is met, then an action (defined by an executable SAS statement) takes place.

Expressions in conditional statements usually involve some kind of comparison. In the following example, type is being compared to a value that is found in the raw data. When the condition is met, the expression is evaluated as true, and the statement following the keyword THEN is executed.

The expression defines a condition so that when the value of type is H, the INPUT statement reads the values for Address. However, when the value of type is not H, the expression is evaluated as false and the INPUT statement is not executed. Notice that the value is enclosed in quotation marks because it is a character value.

     data perm.people;
        infile census;
        retain Address;
        input type $1. @;
        if type='H' then input @3 Address $15.; 

Caution: When you are comparing values, it is important to make sure that the values are expressed exactly as they are coded in the data. For example, the expression below would be evaluated as false because the values in the data are stored in uppercase letters.
if type='h' then ... ; 

back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement