SET Statement


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) )>>

  • < options >;

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

  • specifies a one-level name, a two-level name , or one of the special SAS data set names .

  • See Also: See SAS Data Sets in SAS Language Reference: Concepts for a description of the levels of SAS data set names and when to use each level.

( data-set-options )

  • specifies actions SAS is to take when it reads variables or observations into the program data vector for processing.

  • See: Refer to Definition of Data Set Options on page 6 for a list of the data set options to use with input data sets.

Options

END= variable

  • creates and names a temporary variable that contains an end-of-file indicator. The variable, which is initialized to zero, is set to 1 when SET reads the last observation of the last data set listed. This variable is not added to any new data set.

  • Restriction: END= cannot be used with POINT=. When random access is used, the END= variable is never set to 1.

  • Featured in: Example 11 on page 1404

KEY= index </UNIQUE>

  • provides nonsequential access to observations in a SAS data set, which are based on the value of an index variable or a key.

  • Range: Specify the name of a simple or a composite index of the data set that is being read.

  • Restriction: KEY= cannot be used with POINT=.

  • Tip: Using the _IORC_ automatic variable in conjunction with the SYSRC autocall macro provides you with more error-handling information than was previously available. When you use the SET statement with the KEY= option, the new automatic variable _IORC_ is created. This automatic variable is set to a return code that shows the status of the most recent I/O operation that is performed on an observation in a SAS data set. If the KEY= value is not found, the _IORC_ variable returns a value that corresponds to the SYSRC autocall macro s mnemonic _DSENOM and the automatic variable _ERROR_ is set to 1.

  • Featured in: Example 7 on page 1403 and Example 8 on page 1403.

  • See Also: For more information, see the description of the autocall macro SYSRC in SAS Macro Language: Reference .

  • See Also: UNIQUE option on page 1400

  • CAUTION:

    • Continuous loops can occur when you use the KEY= option. If you use the KEY= option without specifying the primary data set, you must include either a STOP statement to stop DATA step processing, or programming logic that uses the _IORC_ automatic variable in conjunction with the SYSRC autocall macro and checks for an invalid value of the _IORC_ variable, or both.

NOBS= variable

  • creates and names a temporary variable whose value is usually the total number of observations in the input data set or data sets. If more than one data set is listed in the SET statement, NOBS= the total number of observations in the data sets that are listed. The number of observations includes those that are marked for deletion but are not yet deleted.

  • Restriction: For certain SAS views, SAS cannot determine the number of observations. In these cases, SAS sets the value of the NOBS= variable to the largest positive integer value that is available in your operating environment.

  • Tip: At compilation time, SAS reads the descriptor portion of each data set and assigns the value of the NOBS= variable automatically. Thus, you can refer to the NOBS= variable before the SET statement. The variable is available in the DATA step but is not added to any output data set.

  • Interaction: The NOBS= and POINT= options are independent of each other.

  • Featured in: Example 10 on page 1403

OPEN =(IMMEDIATE DEFER)

  • allows you to delay the opening of any concatenated SAS data sets until they are ready to be processed .

    • IMMEDIATE

      • during the compilation phase, opens all data sets that are listed in the SET statement.

      • Restriction: When you use the IMMEDIATE option KEY=, POINT=, and BY statement processing are mutually exclusive.

      • Tip: If a variable on a subsequent data set is of a different type (character versus numeric, for example) than that of the same-named variable on the first data set, the DATA step will stop processing and produce an error message.

    • DEFER

      • opens the first data set during the compilation phase, and opens subsequent data sets during the execution phase. When the DATA step reads and processes all observations in a data set, it closes the data set and opens the next data set in the list.

      • Restriction: When you specify the DEFER option, you cannot use the KEY= statement option, the POINT= statement option, or the BY statement. These constructs imply either random processing or interleaving of observations from the data sets, which is not possible unless all data sets are open.

      • Requirement: You can use the DROP=, KEEP=, or RENAME= data set options to process a set of variables, but the set of variables that are processed for each data set must be identical. In most cases, if the set of variables defined by any subsequent data set differs from that defined by the first data set, SAS prints a warning message to the log but does not stop execution. Exceptions to this behavior are

        1. If a variable on a subsequent data set is of a different type (character versus numeric, for example) than that of the same-named variable on the first data set, the DATA step will stop processing and produce an error message.

        2. If a variable on a subsequent data set was not defined by the first data set in the SET statement, but was defined previously in the DATA step program, the DATA step will stop processing and produce an error message. In this case, the value of the variable in previous iterations may be incorrect because the semantic behavior of SET requires this variable to be set to missing when processing the first observation of the first data set.

    • Default: IMMEDIATE

  • POINT= variable

    • specifies a temporary variable whose numeric value determines which observation is read. POINT= causes the SET statement to use random (direct) access to read a SAS data set.

    • Requirement: a STOP statement

    • Restriction: You cannot use POINT= with a BY statement, a WHERE statement, or a WHERE= data set option. In addition, you cannot use it with transport format data sets, data sets in sequential format on tape or disk, and SAS/ACCESS views or the SQL procedure views that read data from external files.

    • Restriction: You cannot use POINT= with KEY=.

    • Tip: You must supply the values of the POINT= variable. For example, you can use the POINT= variable as the index variable in some form of the DO statement.

    • Tip: The POINT= variable is available anywhere in the DATA step, but it is not added to any new SAS data set.

    • Featured in: Example 6 on page 1403 and Example 9 on page 1403

    • CAUTION:

      • Continuous loops can occur when you use the POINT= option. When you use the POINT= option, you must include a STOP statement to stop DATA step processing, programming logic that checks for an invalid value of the POINT= variable, or both. Because POINT= reads only those observations that are specified in the DO statement, SAS cannot read an end-of-file indicator as it would if the file were being read sequentially. Because reading an end-of-file indicator ends a DATA step automatically, failure to substitute another means of ending the DATA step when you use POINT= can cause the DATA step to go into a continuous loop. If SAS reads an invalid value of the POINT= variable, it sets the automatic variable _ERROR_ to 1. Use this information to check for conditions that cause continuous DO-loop processing, or include a STOP statement at the end of the DATA step, or both.

  • UNIQUE

    • causes a KEY= search always to begin at the top of the index for the data set that is being read.

    • Restriction: UNIQUE can only appear with the KEY= argument and must be preceded by a slash.

    • Explanation: By default, SET begins searching at the top of the index only when the KEY= value changes. If the KEY= value does not change on successive executions of the SET statement, the search begins by following the most recently retrieved observation. In other words, when consecutive duplicate KEY= values appear, the SET statement attempts a one-to-one match with duplicate indexed values in the data set that is being read. If more consecutive duplicate KEY= values are specified than exist in the data set that is being read, the extra duplicates are treated as not found.

    • Featured in: Example 8 on page 1403

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

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

  • reading observations and variables from existing SAS data sets for further processing in the DATA step

  • concatenating and interleaving data sets, and performing one-to-one reading of data sets

  • reading SAS data sets by using direct access methods .

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

  • SET reads an observation from an existing SAS data set. INPUT reads raw data from an external file or from in-stream data lines in order to create SAS variables and observations.

  • Using the KEY= option with SET enables you to access observations nonsequentially in a SAS data set according to a value. Using the POINT= option with SET enables you to access observations nonsequentially in a SAS data set according to the observation number.

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:

  • BY Statement on page 1112

  • DO Statement on page 1143

  • INPUT Statement on page 1245

  • MERGE Statement on page 1304

  • STOP Statement on page 1405

  • UPDATE Statement on page 1414

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




SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 704

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net