SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3

Reads an observation from one or more SAS data sets

Valid: in a DATA step

Category: File-handling

Type: Executable

Syntax

SET < SAS-data-set(s) <( data-set-options(s) )>>

Without Arguments

When you do not specify an argument, the SET statement reads an observation from the most recently created data set.

Arguments

SAS-data-set

( data-set-options )

Options

END= variable

KEY= index </UNIQUE>

NOBS= variable

OPEN =(IMMEDIATE DEFER)

Details

What SET Does Each time the SET statement is executed, SAS reads one observation into the program data vector. SET reads all variables and all observations from the input data sets unless you tell SAS to do otherwise . A SET statement can contain multiple data sets; a DATA step can contain multiple SET statements. See Combining and Modifying SAS Data Sets: Examples .

Uses The SET statement is flexible and has a variety of uses in SAS programming. These uses are determined by the options and statements that you use with the SET statement. They include

BY- Group Processing with SET Only one BY statement can accompany each SET statement in a DATA step. The BY statement should immediately follow the SET statement to which it applies. The data sets that are listed in the SET statement must be sorted by the values of the variables that are listed in the BY statement, or they must have an appropriate index. SET when it is used with a BY statement interleaves data sets. The observations in the new data set are arranged by the values of the BY variable or variables, and within each BY group, by the order of the data sets in which they occur. See Example 2 on page 1402 for an example of BY-group processing with the SET statement.

Combining SAS Data Sets Use a single SET statement with multiple data sets that are specified to concatenate the specified data sets. That is, the number of observations in the new data set is the sum of the number of observations in the original data sets, and the order is all the observations from the first data set followed by all observations from the second data set, and so on. See Example 1 on page 1402 for an example of concatenating data sets.

Use a single SET statement with a BY statement to interleave the specified data sets. The observations in the new data set are arranged by the values of the BY variable or variables, and within each BY group, by the order of the data sets in which they occur. See Example 2 on page 1402 for an example of interleaving data sets.

Use multiple SET statements to perform one-to-one reading (also called one-to-one matching) of the specified data sets. The new data set contains all the variables from all the input data sets. The number of observations in the new data set is the number of observations in the smallest original data set. If the data sets contain common variables, the values that are read in from the last data set replace those read in from earlier ones. See Example 6 on page 1403, Example 7 on page 1403, and Example 8 on page 1403 for examples of one-to-one reading of data sets.

For extensive examples, see Combining and Modifying SAS Data Sets: Examples .

Comparisons

Examples

Example 1: Concatenating SAS Data Sets

If more than one data set name appears in the SET statement, the resulting output data set is a concatenation of all the data sets that are listed. SAS reads all observations from the first data set, then all from the second data set, and so on until all observations from all the data sets have been read. This example concatenates the three SAS data sets into one output data set named FITNESS:

data fitness; set health exercise well; run;

Example 2: Interleaving SAS Data Sets

To interleave two or more SAS data sets, use a BY statement after the SET statement:

data april; set payable recvable; by account; run;

Example 3: Reading a SAS Data Set

In this DATA step, each observation in the data set NC. MEMBERS is read into the program data vector. Only those observations whose value of CITY is Raleigh are output to the new data set RALEIGH.MEMBERS:

data raleigh.members; set nc.members; if city='Raleigh'; run;

Example 4: Merging a Single Observation with All Observations in a SAS Data Set

An observation to be merged into an existing data set can be one that is created by a SAS procedure or another DATA step. In this example, the data set AVGSALES has only one observation:

data national; if _n_=1 then set avgsales; set totsales; run;

Example 5: Reading from the Same Data Set More Than Once

In this example, SAS treats each SET statement independently; that is, it reads from one data set as if it were reading from two separate data sets:

data drugxyz; set trial5(keep=sample); if sample>2; set trial5; run;

For each iteration of the DATA step, the first SET statement reads one observation. The next time the first SET statement is executed, it reads the next observation. Each SET statement can read different observations with the same iteration of the DATA step.

Example 6: Combining One Observation with Many

You can subset observations from one data set and combine them with observations from another data set by using direct access methods, as follows :

data south; set revenue; if region=4; set expense point=_n_; run;

Example 7: Performing a Table Lookup

This example illustrates using the KEY= option to perform a table lookup. The DATA step reads a primary data set that is named INVTORY and a lookup data set that is named PARTCODE. It uses the index PARTNO to read PARTCODE nonsequentially, by looking for a match between the PARTNO value in each data set. The purpose is to obtain the appropriate description, which is available only in the variable DESC in the lookup data set, for each part that is listed in the primary data set:

data combine; set invtory(keep=partno instock price); set partcode(keep=partno desc) key=partno; run;

Example 8: Performing a Table Lookup When the Master File Contains Duplicate Observations

This example uses the KEY= option to perform a table lookup. The DATA step reads a primary data set that is named INVTORY, which is indexed on PARTNO, and a lookup data set named PARTCODE. PARTCODE contains quantities of new stock (variable NEW_STK). The UNIQUE option ensures that, if there are any duplicate observations in INVTORY, values of NEW_STK are added only to the first observation of the group:

data combine; set partcode(keep=partno new_stk); set invtory(keep=partno instock price) key=partno/unique; instock=instock+new_stk; run;

Example 9: Reading a Subset by Using Direct Access

These statements select a subset of 50 observations from the data set DRUGTEST by using the POINT= option to access observations directly by number:

data sample; do obsnum=1 to 100 by 2; set drugtest point=obsnum; if _error_ then abort; output; end; stop; run;

Example 10: Performing a Function Until the Last Observation Is Reached

These statements use NOBS= to set the termination value for DO-loop processing. The value of the temporary variable LAST is the sum of the observations in SURVEY1 and SURVEY2:

do obsnum=1 to last by 100; set survey1 survey2 point=obsnum nobs=last; output; end; stop;

Example 11: Writing an Observation Only After All Observations Have Been Read

This example uses the END= variable LAST to tell SAS to assign a value to the variable REVENUE and write an observation only after the last observation of RENTAL has been read:

set rental end=last; totdays + days; if last then do; revenue=totdays*65.78; output; end;

See Also

Statements:

Rules for Words and Names in SAS Language Reference: Concepts

Reading, Modifying, and Combining SAS Data Sets in SAS Language Reference: Concepts

Definition of Data Set Options on page 6

SAS Macro Language: Reference

Combining and Modifying SAS Data Sets: Examples

Категории