Chapter Contents

Previous

Next

SAS Indexes


Definition of SAS Indexes

An index is an optional file that you can create for a SAS data file to provide direct access to specific observations. The index stores values in ascending value order for a specific variable or variables and includes information as to the location of those values within observations in the data file. In other words, an index allows you to locate an observation by value.

For example, suppose you want the observation with SSN (social security number) equal to 465-33-8613:

You can either create an index when you create a data file, or create an index for an existing data file. The data file can be either compressed or uncompressed. For each data file, you can create one or multiple indexes. Once an index exists, SAS treats it as part of the data file. That is, if you add or delete observations or modify values, the index is automatically updated.


Benefits of an Index

In general, SAS can use an index to improve performance in the following situations:

In addition, an index can benefit other areas of the SAS System. In SCL (SAS Component Language), an index improves the performance of table lookup operations. For the SQL procedure, an index enables the software to process certain classes of queries more efficiently, for example, join queries. For the SAS/IML software, you can explicitly specify that an index be used for read, delete, list, or append operations.

Even though an index can reduce the time required to locate a set of observations, especially for a large data file, there are costs associated with creating, storing, and maintaining the index. When deciding whether to create an index, you must consider increased resource usage, along with the performance improvement.

Note:   An index is never used for the subsetting IF statement in a DATA step or for the FIND and SEARCH commands in the FSEDIT procedure.  [cautionend]


Index File

The index file is a SAS file, which has the same name as its associated data file and a member type of INDEX. There is only one index file per data file; all indexes for a data file are stored in a single file.

The index file may show up as a separate file or appear to be part of the data file, depending on the operating environment. In any case, the index file is stored in the same SAS data library as its data file.

The index file consists of entries that are organized hierarchically and connected by pointers, all of which are maintained by SAS. The lowest level in the index file hierarchy consists of entries that represent each distinct value for an indexed variable, in ascending value order. Each entry consists of

That is, in an index file, each value is followed by one or more RIDs, which identifies the observation(s) in the data file containing the value. (Multiple RIDs result from multiple occurrences of the same value.) For example, the following represents index file entries for the variable LASTNAME:

   Avery          10
   Brown          6,22,43
   Craig          5,50
   Dunn           1

When an index is used to process a request, such as a WHERE expression, SAS does a binary search on the index file and positions the index to the first entry that contains a qualified value. SAS then uses the value's RID(s) to read the observation(s) that contain the value. Subsequent entries' higher (greater) than the requested value are found by reading the remaining entries and then following the pointers to entries that contain higher values. The result is that SAS can quickly locate the observations that are associated with a value or range of values. For example, using an index to process the WHERE expression,

where age > 20 and age < 35;
SAS positions the index to the index entry for the first value greater than 20 and uses the value's RID(s) to read the observation(s). SAS then moves sequentially through the index entries reading observations until it reaches the index entry for the value that is equal to or greater than 35.

SAS automatically keeps the index file balanced as updates are made, which means that it ensures a uniform cost to access any index entry, and all space that is occupied by deleted values is recovered and reused.


Types of Indexes

When you create an index, you designate which variable(s) to index. An indexed variable is called a key variable. You can create two types of indexes:

In addition to deciding whether you want a simple index or a composite index, you can also limit an index (and its data file) to unique values and exclude from the index missing values.

Simple Index

The most common index is a simple index, which is an index of values for one key variable. The variable can be numeric or character. When you create a simple index, SAS assigns to the index the name of the key variable.

The following example shows the DATASETS procedure statements that are used to create two simple indexes for variables CLASS and MAJOR in data file COLLEGE.SURVEY:

proc datasets library=college;
   modify survey;
      index create class;
      index create major;
run;

To process a WHERE expression using an index, SAS uses only one index. When the WHERE expression has multiple conditions using multiple key variables, SAS determines which condition qualifies the smallest subset. For example, suppose that COLLEGE.SURVEY contains the following data:

With simple indexes on CLASS and MAJOR, SAS would select MAJOR to process the following WHERE expression:

where class=97 and major='Biology';

Composite Index

A composite index is an index of two or more key variables with their values concatenated to form a single value. The variables can be numeric, character, or a combination. An example is a composite index for the variables LASTNAME and FRSTNAME. A value for this index is composed of the value for LASTNAME immediately followed by the value for FRSTNAME from the same observation. When you create a composite index, you must specify a unique index name.

The following example shows the DATASETS procedure statements that are used to create a composite index for the data file COLLEGE.MAILLIST, specifying two key variables: ZIPCODE and SCHOOLID.

proc datasets library=college;
   modify maillist;
      index create zipid=(zipcode schoolid);
run;

Often, only the first variable of a composite index is used. For example, for a composite index on ZIPCODE and SCHOOLID, the following WHERE expression can use the composite index for the variable ZIPCODE because it is the first key variable in the composite index:

where zipcode = 78753;

However, you can take advantage of all key variables in a composite index by the way you construct the WHERE expression, which is referred to as compound optimization. Compound optimization is the process of optimizing multiple conditions on multiple variables, which are joined with a logical operator such as AND, using a composite index. If you issue the following WHERE expression, the composite index is used to find all occurrences of ZIPCODE='78753' and SCHOOLID='55'. In this way, all of the conditions are satisfied with a single search of the index:

where zipcode = 78753 and schoolid = 55;

When you are deciding whether to create a simple index or a composite index, consider how you will access the data. If you often access data for a single variable, a simple index will do. But if you frequently access data for multiple variables, a composite index could be beneficial.

Unique Values

Often it is important to require that values for a variable be unique, like social security number and employee number. You can declare unique values for a variable by creating an index for the variable and including the UNIQUE option. A unique index guarantees that values for one variable or the combination of a composite group of variables remain unique for every observation in the data file. If an update tries to add a duplicate value to that variable, the update is rejected.

The following example creates a simple index for the variable IDNUM and requires that all values for IDNUM be unique:

proc datasets library=college;
   modify student;
      index create idnum / unique;
run;

Missing Values

If a variable has a large number of missing values, it may be desirable to keep them from using space in the index. Therefore, when you create an index, you can include the NOMISS option to specify that missing values are not maintained by the index.

The following example creates a simple index for the variable RELIGION and specifies that the index does not maintain missing values for the variable:

proc datasets library=college;
   modify student;
      index create religion / nomiss;
run;

In contrast to the UNIQUE option, observations with missing values for the key variable can be added to the data file, even though the missing values are not added to the index.

SAS will not use an index that was created with the NOMISS option to process a BY statement or to process a WHERE expression that qualifies observations containing missing values. For example, suppose the index AGE was created with the NOMISS option and observations exist that contain missing values for the variable AGE. SAS will not use the index for the following:

proc print data=mydata.employee;
   where age < 35;
run;


Deciding Whether to Create an Index

Costs of an Index

An index exists to improve performance. However, an index conserves some resources at the expense of others. Therefore, you must consider costs associated with creating, using, and maintaining an index. The following topics provide information on resource usage and give you some guidelines for creating indexes.

When you are deciding whether to create an index, you must consider CPU cost, I/O cost, buffer requirements, and disk space requirements.

CPU Cost

Additional CPU time is necessary to create an index as well as to maintain the index when the data file is modified. That is, for an indexed data file, when a value is added, deleted, or modified, it must also be added, deleted, or modified in the appropriate index(es).

When SAS uses an index to read an observation from a data file, there is also increased CPU usage. The increased usage results from SAS using a more complicated process than is used when SAS retrieves data sequentially. Although CPU usage is greater, you benefit from SAS reading only those observations that meet the conditions. Note that this is why using an index is more expensive when there is a larger number of observations that meet the conditions.

Note:   To compare CPU usage with and without an index, for some operating environments, you can issue the STIMER or FULLSTIMER system options to write performance statistics to the SAS log.  [cautionend]

I/O Cost

Using an index to read observations from a data file may increase the number of I/O (input/output) requests compared to reading the data file sequentially. For example, processing a BY statement with an index may increase I/O count, but you save in not having to issue the SORT procedure. For WHERE processing, SAS considers I/O count when deciding whether to use an index.

To process a request using an index, the following occurs:

  1. SAS does a binary search on the index file and positions the index to the first entry that contains a qualified value.

  2. SAS uses the value's RID (identifier) to directly access the observation containing the value. SAS transfers the observation between external storage to a buffer, which is the memory into which data is read or from which data is written. The data is transferred in pages, which is the amount of data (the number of observations) that can be transferred for one I/O request; each data file has a specified page size.

  3. SAS then continues the process until the WHERE expression is satisfied. Each time SAS accesses an observation, the data file page containing the observation must be read into memory if it is not already there. Therefore, if the observations are on multiple data file pages, an I/O operation is performed for each observation.

The result is that the more random the data, the more I/Os are required to use the index. If the data is ordered more like the index, which is in ascending value order, fewer I/Os are required to access the data.

The number of buffers determines how many pages of data can simultaneously be in memory. Frequently, the larger the number of buffers, the fewer number of I/Os will be required. For example, if the page size is 4096 bytes and one buffer is allocated, then one I/O transfers 4096 bytes of data (or one page). To reduce I/Os, you can increase the page size but you will need a larger buffer. To reduce the buffer size, you can decrease the page size but you will use more I/Os.

For information on data file characteristics like the data file page size and the number of data file pages, issue the CONTENTS procedure (or use the CONTENTS statement in the DATASETS procedure). With this information, you can determine the data file page size and experiment with different sizes. Note that the information that is available from PROC CONTENTS depends on the operating environment.

The BUFSIZE= data set option (or system option) sets the page size for a data file when it is created. The BUFNO= data set option (or system option) specifies how many buffers to allocate for a data file and for the overall system for a given execution of SAS; that is, BUFNO= is not stored as a data set attribute.

Buffer Requirements

In addition to the resources that are used to create and maintain an index, SAS also requires additional memory for buffers when an index is actually used. Opening the data file opens the index file but none of the indexes. The buffers are not required unless SAS uses the index but they must be allocated in preparation for the index that is being used. The number of buffers that are allocated depends on the number of levels in the index tree and in the data file open mode. If the data file is open for input, the maximum number of buffers is three; for update, the maximum number is four. (Note that these buffers are available for other uses; they are not dedicated to indexes.)

Disk Space Requirements

Additional disk space is required to store the index file, which may show up as a separate file or may appear to be part of the data file, depending on the operating environment.

For information on the index file size, issue the CONTENTS procedure (or the CONTENTS statement in the DATASETS procedure). Note that the available information from PROC CONTENTS depends on the operating environment.


Guidelines for Creating Indexes

Data File Considerations


Index Use Considerations


Key Variable Candidates

In most cases, multiple variables are used to query a data file. However, it probably would be a mistake to index all variables in a data file, as certain variables are better candidates than others:


Methods of Creating an Index

You can create one index for a data file, which can be either a simple index or a composite index, or you can create multiple indexes, which can be multiple simple indexes, multiple composite indexes, or a combination of both simple and composite. In general, the process of creating an index is as follows:

  1. You request to create an index for one or multiple variables using a method such as the INDEX CREATE statement in the DATASETS procedure.

  2. SAS reads the data file one observation at a time, extracts values and RID(s) for each key variable, and places them in the index file.

  3. SAS then examines the data file to determine if the data is already sorted by the key variable(s) in ascending order. SAS looks in the data file for its sort assertion, which is determined from a previous SORT procedure or from a SORTEDBY= data set option:

    Note:   If a data file's sort assertion is set from a SORTEDBY= data set option, SAS validates that the data is sorted as specified by the data set option. If the data is not sorted appropriately, the index will not be created, and a message displays telling you that the index was not created because values are not sorted in ascending order.  [cautionend]

Methods to create an index are briefly described in this section; for details, refer to the INDEX= data set option in the SAS Language Reference: Dictionary.

Using the DATASETS Procedure

The DATASETS procedure provides statements that allow you to create and delete indexes. In the following example, the MODIFY statement identifies the data file, the INDEX DELETE statement deletes two indexes, and the two INDEX CREATE statements specify the variables to index, with the first INDEX CREATE statement specifying the options UNIQUE and NOMISS:

proc datasets library=mylib;
modify employee;
   index delete salary age;
   index create empnum / unique nomiss;
   index create names=(lastname frstname);

Note:   If you delete and create indexes in the same step, place the INDEX DELETE statement before the INDEX CREATE statement so that space occupied by deleted indexes can be reused during index creation.  [cautionend]

Using the INDEX= Data Set Option

To create indexes in a DATA step when you create the data file, use the INDEX= data set option. The INDEX= data set option also allows you to include the NOMISS and UNIQUE options. The following example creates a simple index on the variable STOCK and specifies UNIQUE:

data finances(index=(stock) /unique);

The next example uses the variables SSN, CITY, and STATE to create a simple index named SSN and a composite index named CITYST:

data employee(index=(ssn cityst=(city state)));

Using the SQL Procedure

The SQL procedure supports index creation and deletion and the UNIQUE option. Note that the variable list requires that variable names be separated by commas (which is an SQL convention) instead of blanks (which is a SAS convention).

The DROP INDEX statement deletes indexes. The CREATE INDEX statement specifies the UNIQUE option, the name of the index, the target data file, and the variable(s) to be indexed. For example:

drop index salary from employee;
create unique index empnum on employee (empnum);
create index names on employee (lastname, frstname);

Using Other SAS Products

You can also create and delete indexes using other SAS utilities and products, such as the SAS Explorer, SAS/IML software, SAS Component Language, and SAS/Warehouse Administrator software.


Using an Index for WHERE Processing

WHERE processing conditionally selects observations for processing when you issue a WHERE expression. Using an index to process a WHERE expression improves performance and is referred to as optimizing the WHERE expression.

To process a WHERE expression, by default SAS decides whether to use an index or read all the observations in the data file sequentially. To make this decision, SAS does the following:

  1. Identifies an available index or indexes.

  2. Estimates the number of observations that would be qualified. If multiple indexes are available, SAS selects the index that returns the smallest subset of observations.

  3. Compares resource usage to decide whether it is more efficient to satisfy the WHERE expression by using the index or by reading all the observations sequentially.


Identifying Available Index or Indexes

The first step for SAS in deciding whether to use an index to process a WHERE expression is to identify if the variable or variables included in the WHERE expression are key variables (that is, have an index). Even though a WHERE expression can consist of multiple conditions specifying different variables, SAS uses only one index to process the WHERE expression. SAS tries to select the index that satisfies the most conditions and selects the smallest subset:

SAS attempts to use an index for the following types of conditions:

WHERE Conditions That Can Be Optimized
Condition Examples
comparison operators, which include the EQ operator; directional comparisons like less than or greater than; and the IN operator where empnum eq 3374;

where empnum < 2000;

where state in ('NC','TX');

comparison operators with NOT where empnum ^= 3374;

where x not in (5,10);

comparison operators with the colon modifier where lastname gt: 'Sm';


CONTAINS operator where lastname contains 'Sm';
fully-bounded range conditions specifying both an upper and lower limit, which includes the BETWEEN-AND operator where 1 < x < 10;

where empnum between 500 and 1000;

pattern-matching operators LIKE and NOT LIKE where frstname like '%Rob_%'
IS NULL or IS MISSING operator where name is null;

where idnum is missing;

TRIM function where trim(state)='Texas';
SUBSTR function in the form of:

WHERE SUBSTR (variable, position, length)='string';

when the following conditions are met:

position is equal to 1, length is less than or equal to the length of variable, and length is equal to the length of string

where substr (name,1,3)='Mac' and (city='Charleston' or city='Atlanta');

The following examples illustrate optimizing a single condition:

Note:   An index is not supported for arithmetic operators, a variable-to-variable condition, and the sounds-like operator.  [cautionend]

Compound Optimization

Compound optimization is the process of optimizing multiple conditions specifying different variables, which are joined with logical operators such as AND or OR, using a composite index. Using a single index to optimize the conditions can greatly improve performance.

For example, suppose you have a composite index for LASTNAME and FRSTNAME. If you issue the following WHERE expression, SAS uses the concatenated values for the first two variables, then SAS further evaluates each qualified observation for the EMPID value:

where lastname eq 'Smith' and frstname eq 'John' and empid=3374;

For compound optimization to occur, all of the following must be true.

Note:   The same conditions that are acceptable for optimizing a single condition are acceptable for compound optimization except for the CONTAINS operator, the pattern-matching operators LIKE and NOT LIKE, and the IS NULL and IS MISSING operators. Also, functions are not supported.  [cautionend]

For the following examples, assume there is a composite index named IJK for variables I, J, and K:

  1. The following conditions are compound optimized because every condition specifies a variable that is in the composite index, and each condition uses one of the supported operators. SAS will position the composite index to the first entry that meets all three conditions and will retrieve only observations that satisfy all three conditions:
    where i = 1 and j not in (3,4) and 10 < k < 12;

  2. This WHERE expression cannot be compound optimized because the range condition for variable I is not fully bounded. In a fully-bounded condition, both an upper and lower bound must be specified. The condition I < 5 only specifies an upper bound. In this case, the composite index can still be used to optimize the single condition I < 5:
    where i < 5 and j in (3,4) and k =3;

  3. For the following WHERE expression, only the first two conditions are optimized with index IJK. After retrieving a subset of observations that satisfy the first two conditions, SAS examines the subset and eliminates any observations that fail to match the third condition.
    where i in (1,4) and j = 5 and k like '%c'l

  4. The following WHERE expression cannot be optimized with index IJK because J and K are not the first two key variables in the composite index:
    where j = 1 and k = 2;

  5. This WHERE expression can be optimized for variables I and J. After retrieving observations that satisfy the second and third conditions, SAS examines the subset and eliminates those observations that do not satisfy the first condition.
    where x < 5 and i = 1 and j = 2;


Estimating the Number of Qualified Observations

Once SAS identifies the index or indexes that can satisfy the WHERE expression, the software estimates the number of observations that will be qualified by an available index. When multiple indexes exist, SAS selects the one that appears to produce the fewest qualified observations.

Starting with Version 7, the software's ability to estimate the number of observations that will be qualified is improved because the software stores additional statistics called cumulative percentiles (or centiles for short). Centiles information represents the distribution of values in an index so that SAS does not have to assume a uniform distribution as in prior releases. To print centiles information for an indexed data file, include the CENTILES option in PROC CONTENTS (or in the CONTENTS statement in the DATASETS procedure).

Note that, by default, SAS does not update centiles information after every data file change. When you create an index, you can include the UPDATECENTILES option to specify when centiles information is updated. That is, you can specify that centiles information be updated every time the data file is closed, when a certain percent of values for the key variable have been changed, or never. In addition, you can also request that centiles information is updated immediately, regardless of the value of UPDATECENTILES, by issuing the INDEX CENTILES statement in PROC DATASETS.

As a general rule, SAS uses an index if it estimates that the WHERE expression will select approximately one-third or fewer of the total number of observations in the data file.

Note:   If SAS estimates that the number of qualified observations is less than 3% of the data file (or if no observations are qualified), SAS automatically uses the index. In other words, in this case, SAS does not bother comparing resource usage.  [cautionend]

Comparing Resource Usage

Once SAS estimates the number of qualified observations and selects the index that qualifies the fewest observations, SAS must then decide if it is faster (cheaper) to satisfy the WHERE expression by using the index or by reading all of the observations sequentially. SAS makes this determination as follows:

This decision is much like a reader deciding whether to use an index at the back of a book. A book's index is designed to allow a reader to locate a topic along with the specific page number(s). Using the index, the reader would go to the specific page number(s) and read only about a specific topic. If the book covers 42 topics and the reader is interested in only a couple of topics, then the index saves time by preventing the reader from reading other topics. However, if the reader is interested in 39 topics, searching the index for each topic would take more time than simply reading the entire book.

To compare resource usage, SAS does the following:

  1. First, SAS predicts the number of I/Os it will take to satisfy the WHERE expression using the index. To do so, SAS positions the index to the first entry that contains a qualified value. In a buffer management simulation that takes into account the current number of available buffers, the RIDs (identifiers) on that index page are processed, indicating how many I/Os it will take to read the observations in the data file.

    If the observations are randomly distributed throughout the data file, the observations will be located on multiple data file pages. This means an I/O will be needed for each page. Therefore, the more random the data in the data file, the more I/Os it takes to use the index. If the data in the data file is ordered more like the index, which is in ascending value order, fewer I/Os are needed to use the index.

  2. Then SAS calculates the I/O cost of a sequential pass of the entire data file and compares the two resource costs.

Factors that affect the comparison include the size of the subset relative to the size of the data file, data file value order, data file page size, the number of allocated buffers, and the cost to uncompress a compressed data file for a sequential read.

Note:   If comparing resource costs results in a tie, SAS chooses the index.  [cautionend]

Controlling WHERE Processing Index Usage with Data Set Options

In Version 7 or later releases, you can control index usage for WHERE processing with the IDXWHERE= and IDXNAME= data set options.

The IDXWHERE= data set option overrides the software's decision regarding whether to use an index to satisfy the conditions of a WHERE expression as follows:

The following example tells SAS to decide which index is the best for optimizing the WHERE expression. SAS will disregard the possibility that a sequential search of the data file might be more resource efficient.

data mydata.empnew;
   set mydata.employee (idxwhere=yes);
   where empnum < 2000;

For details, see the IDXWHERE data set option in SAS Language Reference: Dictionary.

The IDXNAME= data set option directs SAS to use a specific index in order to satisfy the conditions of a WHERE expression.

By specifying IDXNAME=index-name, you are specifying the name of a simple or composite index for the data file.

The following example uses the IDXNAME= data set option to direct SAS to use a specific index to optimize the WHERE expression. SAS will disregard the possibility that a sequential search of the data file might be more resource efficient and does not attempt to determine if the specified index is the best one. (Note that the EMPNUM index was not created with the NOMISS option.)

data mydata.empnew;
   set mydata.employee (idxname=empnum);
   where empnum < 2000;

For details, see the IDXNAME data set option in SAS Language Reference: Dictionary.

Displaying Index Usage Information in the SAS Log

To display information in the SAS log regarding index usage, change the value of the MSGLEVEL= system option from its default value of N to I. When you issue options msglevel=i;, the following occurs:


Using an Index with Views

You cannot create an index for a data view; it must be a data file. However, if a data view is created from an indexed data file, index usage is available. That is, if the view definition includes a WHERE expression using a key variable, then SAS will attempt to use the index. Additionally, there are other ways to take advantage of a key variable when using a view.

In this example, you create an SQL view named STAT from data file CRIME, which has the key variable STATE. In addition, the view definition includes a WHERE expression:

proc sql;
   create view stat as
   select * from crime
   where murder > 7;
quit;

If you issue the following PRINT procedure, which refers to the SQL view, along with a WHERE statement that specifies the key variable STATE, SAS cannot optimize the WHERE statement with the index. SQL views cannot join a WHERE expression that was defined in the view to a WHERE expression that was specified in another procedure, DATA step, or SCL:

proc print data=stat;
   where state > 42;
run;

However, if you issue PROC SQL with an SQL WHERE clause that specifies the key variable STATE, then the SQL view can join the two conditions, which allows SAS to use the index STATE:

proc sql;
select * from stat where state > 42;
quit;


Using an Index for BY Processing

BY processing allows you to process observations in a specific order according to the values of one or more variables that are specified in a BY statement. Indexing a data file enables you to use a BY statement without sorting the data file. By creating an index based on one or more variables, you can ensure that observations are processed in ascending numeric or character order. Simply specify in the BY statement the variable or list of variables that are indexed.

For example, if an index exists for LASTNAME, the following BY statement would use the index to order the values by last names:

proc print;
   by lastname;

When you specify a BY statement, SAS looks for an appropriate index. If one exists, the software automatically retrieves the observations from the data file in indexed order.

A BY statement will use an index in the following situations:

For example, if the variable MAJOR has a simple index, the following BY statements use the index to order the values by MAJOR:

by major;
by major state;

If a composite index named ZIPID exists consisting of the variables ZIPCODE and SCHOOLID, the following BY statements use the index:

by zipcode;
by zipcode schoolid;
by zipcode schoolid name;

However, the composite index ZIPID is not used for these BY statements:

by schoolid;
by schoolid zipcode;

In addition, a BY statement will not use an index in these situations:

Note:   Using an index to process a BY statement may not always be more efficient than simply sorting the data file, particularly if the data file has a high blocking factor of observations per page. Therefore, using an index for a BY statement is generally for convenience, not performance.  [cautionend]


Using an Index for Both WHERE and BY Processing

If both a WHERE expression and a BY statement are specified, SAS looks for one index that satisfies requirements for both. If such an index is not found, the BY statement takes precedence.

With a BY statement, SAS cannot use an index to optimize a WHERE expression if the optimization would invalidate the BY order. For example, the following statements could use an index on the variable LASTNAME to optimize the WHERE expression because the order of the observations returned by the index does not conflict with the order required by the BY statement:

proc print;
  by lastname;
  where lastname >= 'Smith';
run;

However, the following statements cannot use an index on LASTNAME to optimize the WHERE expression because the BY statement requires that the observations be returned in EMPID order:

proc print;
   by empid;
   where lastname = 'Smith';
run;


Specifying an Index with the KEY= Option for SET and MODIFY Statements

The SET and MODIFY statements provide the KEY= option, which allows you to specify an index in a DATA step to retrieve particular observations in a data file.

The following MODIFY statement shows how to use the KEY= option to take advantage of the fact that the data file INVTY.STOCK has an index on the variable PARTNO. Using the KEY= option tells SAS to use the index to directly access the correct observations to modify.

modify invty.stock key=partno;

Note:   A BY statement is not allowed in the same DATA step with the KEY= option, and WHERE processing is not allowed for a data file with the KEY= option.  [cautionend]


Taking Advantage of an Index

Applications that typically do not use indexes can be rewritten to take advantage of an index. For example:


Maintaining Indexes

SAS provides several procedures that you can issue to maintain indexes, and there are several operations within SAS that automatically maintain indexes for you.

Displaying Data File Information

The CONTENTS procedure (or the CONTENTS statement in PROC DATASETS) reports the following types of information.

Note:   The available information depends on the operating environment.  [cautionend]

Output of PROC CONTENTS
                                             The SAS System              

                                         The CONTENTS Procedure

             Data Set Name: SASUSER.STAFF                        Observations:         148
             Member Type:   DATA                                 Variables:            6
             Engine:        V8                                   Indexes:              2
             Created:       9:59 Tuesday, May 11, 1999           Observation Length:   63
             Last Modified: 10:03 Tuesday, May 11, 1999          Deleted Observations: 0
             Protection:                                         Compressed:           NO
             Data Set Type:                                      Sorted:               NO
             Label:


                              -----Engine/Host Dependent Information-----

       Data Set Page Size:         8192
       Number of Data Set Pages:   3
       First Data Page:            1
       Max Obs per Page:           129
       Obs in First Data Page:     104
       Index File Page Size:       8192

                                             The SAS System              

                                         The CONTENTS Procedure

                              -----Engine/Host Dependent Information-----

       Number of Index File Pages: 3
       Number of Data Set Repairs: 0
       File Name:                  /remote/obi01/wan0.2/u/sasXXX/sasuser.devn/staff.sas7bdat
       Release Created:            8.00.00B
       Host Created:               HP-UX
       Inode Number:               237883
       Access Permission:          rw-r--r--
       Owner Name:                 XXXXXX
       File Size (bytes):          32768


                                             The SAS System              

                                         The CONTENTS Procedure

                         -----Alphabetic List of Variables and Attributes-----

                                  #    Variable    Type    Len    Pos
                                  -----------------------------------
                                  4    city        Char     15     34
                                  3    fname       Char     15     19
                                  6    hphone      Char     12     51
                                  1    idnum       Char      4      0
                                  2    lname       Char     15      4
                                  5    state       Char      2     49


                                             
                                      The SAS System              

                                         The CONTENTS Procedure

                           -----Alphabetic List of Indexes and Attributes-----

                                            Current      # of
                      Unique      Update     Update    Unique
        #    Index    Option    Centiles    Percent    Values    Variables
        ----------------------------------------------------------------------------------------
        1    idnum    YES              5          0       148
             ---                                                 1009
             ---                                                 1065
             ---                                                 1105
             ---                                                 1115
             ---                                                 1123
             ---                                                 1130
             ---                                                 1221
             ---                                                 1352
             ---                                                 1385
             ---                                                 1405
             ---                                                 1412

                                             The SAS System              

                                         The CONTENTS Procedure

                           -----Alphabetic List of Indexes and Attributes-----

                                            Current      # of
                      Unique      Update     Update    Unique
        #    Index    Option    Centiles    Percent    Values    Variables
        ----------------------------------------------------------------------------------------
             ---                                                 1421
             ---                                                 1429
             ---                                                 1436
             ---                                                 1475
             ---                                                 1521
             ---                                                 1616
             ---                                                 1739
             ---                                                 1845
             ---                                                 1919
             ---                                                 1995
        2    names                     5          0       148    fname lname
             ---                                                 ABDULLAH       ,ALHERTANI

                                             The SAS System              

                                         The CONTENTS Procedure

                           -----Alphabetic List of Indexes and Attributes-----

                                            Current      # of
                      Unique      Update     Update    Unique
        #    Index    Option    Centiles    Percent    Values    Variables
        ----------------------------------------------------------------------------------------
             ---                                                 ALICE          ,MURPHY
             ---                                                 ANTHONY        ,COOPER
             ---                                                 CAROL          ,PEARCE
             ---                                                 CLYDE          ,HERRERO
             ---                                                 DIANE          ,NORRIS
             ---                                                 ELIZABETH      ,VARNER
             ---                                                 GRETCHEN       ,HOWARD
             ---                                                 JAKOB          ,BREWCZAK
             ---                                                 JEFF           ,LI
             ---                                                 JOHN           ,MARKS
             ---                                                 JULIA          ,RODRIGUEZ
             ---                                                 LARRY          ,UPCHURCH
                                             The SAS System              

                                         The CONTENTS Procedure

                           -----Alphabetic List of Indexes and Attributes-----

                                            Current      # of
                      Unique      Update     Update    Unique
        #    Index    Option    Centiles    Percent    Values    Variables
        ----------------------------------------------------------------------------------------
             ---                                                 LEVI           ,GOLDSTEIN
             ---                                                 MARY           ,PARKER
             ---                                                 NADINE         ,WELLS
             ---                                                 RANDY          ,SANYERS
             ---                                                 ROGER          ,DENNIS
             ---                                                 SANDRA         ,NEWKIRK
             ---                                                 THOMAS         ,BURNETTE
             ---                                                 WILLIAM        ,PHELPS


Copying an Indexed Data File

When you copy an indexed data file with the COPY procedure (or the COPY statement of the DATASETS procedure), you can specify whether the procedure also recreates the index file for the new data file with the INDEX=YES|NO option; the default is YES, which recreates the index. However, recreating the index does increase the processing time for the PROC COPY step.

If you copy from disk to disk, the index is recreated. If you copy from disk to tape, the index is not recreated on tape. However, after copying from disk to tape, if you then copy back from tape to disk, the index can be recreated. Note that if you move a data file with the MOVE option in PROC COPY, the index file is deleted from IN= library and recreated in OUT= library.

The CPORT procedure also has INDEX=YES|NO to specify whether to export indexes with indexed data files. By default, PROC CPORT exports indexes with indexed data files. The CIMPORT procedure, however, does not handle the index file at all, and the index(es) must be recreated.

Updating an Indexed Data File

Each time that values in an indexed data file are added, modified, or deleted, SAS automatically updates the index. The following activities affect an index as indicated:

Maintenance Tasks and Index Results
Task Result
delete a data set index file is deleted
rename a data set index file is renamed
rename key variable simple index is renamed
delete key variable simple index is deleted
add observation index entries are added
delete observations index entries are deleted and space is recovered for resuse
update observations index entries are deleted and new ones are inserted

Note:   Use the SAS System to perform additions, modifications and deletions to your data sets. Using operating system commands to perform these operations will make your files unusable.  [cautionend]

Sorting an Indexed Data File

You can sort an indexed data file only if you direct the output of the SORT procedure to a new data file so that the original data file remains unchanged. However, the new data file is not automatically indexed.

Note:   If you sort an indexed data file with the FORCE option, the index file is deleted.  [cautionend]

Adding Observations to an Indexed Data File

Adding observations to an indexed data file requires additional processing. SAS automatically keeps the values in the index consistent with the values in the data file.

Multiple Occurrences

An index that is created without the UNIQUE option can result in multiple occurrences of the same value, which results in multiple RIDs for one value. For large data files with many multiple occurrences, the list of RIDs for a given value may require several pages in the index file. Because the RIDs are stored in physical order, any new observation added to the data file with the given value is stored at the end of the list of RIDs. Navigating through the index to find the end of the RID list can cause many I/O operations.

In Version 7 and later releases, SAS remembers the previous position in the index so that when inserting more occurrences of the same value, the end of the RID list is found quickly.

Appending to an Indexed Data File

Version 7 and later releases provide performance improvements when appending a data file to an indexed data file. SAS suspends index updates until all observations are added, then updates the index with data from the newly added observations. See the APPEND statement in the DATASETS procedure in SAS Language Reference: Dictionary.

Recovering a Damaged Index

An index can become damaged for many of the same reasons that a data file or catalog can become damaged. If a data file becomes damaged, use the REPAIR statement in PROC DATASETS to repair the data file or recreate any missing indexes. For example,

proc datasets library=mylib;
   repair mydata;
run;


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.