Using Indexes

Overview

An index can help you quickly locate one or more observations that you want to read. Indexes provide direct access to observations in SAS data sets based on values of one or more key variables. Index applications include the following:
  • yield faster access to small subsets of observations for WHERE processing
  • perform table lookup operations
  • join observations
Without an index, SAS accesses observations sequentially, in the order in which they are stored in a data set. For example, to access the observation in the sample SAS data set shown below that has a value of Smith for the variable Name, SAS begins with the first observation and reads through each one until it reaches the observation that satisfies the condition.
sample SAS data set
When you index a SAS data set, SAS creates an index file that stores values in ascending value order for a specific variable or variables and includes information about the location of those values in the data file. That is, an index consists of value/identifier pairs that enable you to locate an observation by value. For example, if you create an index on the sample SAS data set that is shown below based on the variable Name, SAS uses the index to find the observation that has a value of Smith for Name directly without having to read all the prior observations.
sample SAS data set

Types of Indexes

You can create two types of indexes:
  • a simple index
  • a composite index
A simple index consists of the values of one key variable, which can be character or numeric. When you create a simple index, SAS names the index after the key variable.
A composite index consists of the values of multiple key variables, which can be character, numeric, or a combination. The values of these key variables are concatenated to form a single value. For example, if an index is built on the key variables Lastname and Firstname, a value for the index includes the value for Lastname followed by the value for Firstname. When you create a composite index, you must specify a unique index name that is not the name of any existing variable or index in the data set.
Often, only the first variable of a composite index is used. In the example, you could use the composite index that is specified in the example above (Lastname plus Firstname) for a WHERE expression that uses only Lastname. For example, the expression where Lastname='Smith' uses the composite index because Lastname is the first variable in the index. That is, the value for Lastname is the first part of the value that is listed in the index.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.181.36