You are on page 1of 21

Acting on Selected

Observations

Chapter 9
HOMEWORK

❑ Find SUBSTRING function in SAS help. Read the documentation


and make yourself familiar with this function.

❑ Show that you have learnt the use of this function by writing a
small SAS program that involves using SUBSTRING function on
a small example data set you create yourself. The data set must
NOT be taken from any SAS help documentation.

❑ Submit to Class RA the SAS program you wrote to demonstrate


use of SUBSRTING function. Also submit the output and SAS
log.

❑ List all SAS character functions and state what tasks may be
performed with them. Description of tasks must be very brief.
SAS data set used in examples

NumberOfEvents= Number of attractions visited during tour


M=Museum,
G=Art gallary
Selecting Observations
Understanding selection process

SAS selects observations through IF-THEN statements. The format


is:

IF condition THEN action;

Examples of conditions:

❑ City = ‘Rome’

❑ NumberOfEvents > Nights

❑ ToursGuide = ‘Lucas’ and Nights > 7


Understanding selection process

Example of actions:
When condition in the IF clause if true, SAS performs action specified
in the THEN clause.

Therefore action must be expressed as SAS statement that


is executable. The example of executable statements are:
Selecting observations based on a simple
conditions
Providing alternative action
Sometimes when a condition in IF statement is false, alternative action must be
provided. This is accomplished with the use of ELSE statement. The syntax is:

ELSE action;
Creating a series of mutually exclusive
conditions

Note: missing value is the lowest possible numeric value


Selecting observations based on multiple simple conditions

Note: Conditions are not necessarily mutually exclusive


Using more than one comparisons in a condition

Case 1: When all condition must be true use AND operator


More then one comparisons (cont.)

Case 2: When only one condition must be true use of OR operator


Using Negative Operators
Negative operator: “not equal to”
Using complex comparisons that require
AND & OR

Note the use of brackets in above program. Also note the double quotes
Abbreviating numeric comparisons
If variablename

For making comparisons:


Comparing characters

Comparing Uppercase & Lowercase Characters


Selecting all values that begin with same group
of characters
Suppose we want to select observations for which name of tour guide starts with ‘D’

Use colon after


Equal to sign (=:)
Selecting a range of character values
The following statements divide names of guides into two groups beginning with
A-L and M-Z by Combining the comparison operator with colon:
Use of INDEX function
Data set for example
Finding a value anywhere within another
character value
Suppose we want to select observations where the variable EventDescription
refers to other attractions.

The position of other in the character string varies from observation to


observation

How can it be determined that other exists anywhere in value of


EventDescription for a given observation?

INDEX function determines whether a specified character string (the excerpt)


is present within a particular character value (source)

INDEX (source, excerpt)


Use of INDEX function

You might also like