For example, either of the two following statements writes to the SAS log, during
each iteration of the DATA step, the contents of an input record in which an input
error is encountered:
if _error_=1 then put _infile_;
if _error_ then put _infile_;
SAS Variable Lists
Definition
A SAS variable list is an abbreviated method of referring to a list of variable names.
SAS enables you to use the following variable lists:
numbered range lists
name range lists
name prefix lists
special SAS name lists
With the exception of the numbered range list, you refer to the variables in a variable list
in the same order that SAS uses to keep track of the variables. SAS keeps track of active
variables in the order in which the compiler encounters them within a DATA step. This
happens whether the active variables are read from existing data sets, an external file, or
created in the step. In a numbered range list, you can refer to variables that were created
in any order, provided that their names have the same prefix.
You can use variable lists in many SAS statements and data set options, including those
that define variables. However, they are especially useful after you define all of the
variables in your SAS program because they provide a quick way to reference existing
groups of data.
Note: Only the numbered range list is used in the RENAME= option.
Numbered Range Lists
Numbered range lists require you to have a series of variables with the same name,
except for the last character or characters, which are consecutive numbers. For example,
the following two lists refer to the same variables:
x1,x2,x3,...,xn
x1-xn
In a numbered range list, you can begin with any number and end with any number as
long as you do not violate the rules for user-supplied variable names and the numbers are
consecutive.
For example, suppose you decide to give some of your numeric variables sequential
names, as in VAR1, VAR2, and so on. Then, you can write an INPUT statement as
follows:
input idnum name $ var1-var3;
Note that the character variable NAME is not included in the abbreviated list.
SAS Variable Lists 49
Name Range Lists
Name range lists rely on the order of variable definition, as shown in the following table:
Table 4.4 Name Range Lists
Variable List Included Variables
x--a
all variables in order of variable definition, from x to a inclusive
x-numeric-a
all numeric variables from x to a inclusive
x-character-a
all character variables from x to a inclusive
You can use the VARNUM option in PROC CONTENTS to print the variables in the
order of definition.
For example, consider the following INPUT statement:
input idnum name $ weight pulse chins;
In later statements, you can use these variable lists:
/* keeps only the numeric variables idnum, weight, and pulse */
keep idnum-numeric-pulse;
/* keeps the consecutive variables name, weight, and pulse */
keep name--pulse;
Name Prefix Lists
Some SAS functions and statements enable you to use a name prefix list to refer to all
variables that begin with a specified character string:
sum(of Sales:)
This character string tells SAS to calculate the sum of all the variables that begin with
“Sales,” such as Sales_Jan, Sales_Feb, and Sales_Mar.
Special SAS Name Lists
Special SAS name lists include:
_NUMERIC_
specifies all numeric variables that are already defined in the current DATA step.
_CHARACTER_
specifies all character variables that are currently defined in the current DATA step.
_ALL_
specifies all variables that are currently defined in the current DATA step.
50 Chapter 4 SAS Variables
The OF Operator with Variable Lists
Definition
The OF operator precedes the arguments of some SAS functions that take variable lists
and arrays as arguments. Here is the general syntax for SAS functions that use the OF
operator:
function-name(OF variable-list) | (OF array-name)
The OF operator is important when used with functions whose arguments are in the form
of a numbered-range list. For example, an argument in the form of a numbered range (x1
– xn) is read in as a range of values only if the list is preceded by the OF operator. If the
same list is not preceded by the OF operator and it is used with the SUM function, the (-)
character is treated as a subtraction sign. The function returns the difference between the
variables rather than the sum of the range of values.
In the following example, arguments are passed in as numbered range lists, both with
and without the use of the OF operator. The first SUM function returns T=20 and the
second SUM function returns
T2=60.
data _null_;
x1=30; x2=20; x3=10;
T=sum(x1-x3);
T2=sum(OF x1-x3);
put T=; /*returns the difference between x1 and x3.*/
Put T2=; /*returns the sum of the variable values from x1 to x3 (inclusive)*/
run;
Note: If you pass in an argument that contains an operation such as subtraction or
addition on data that contains missing values, SAS returns a missing value and no
calculation is made.
You can also use the OF operator to pass array names into functions as shown here:
varA=mean(of array-name[*]);
For more information about using the OF operator with arrays, see “Using the OF
Operator with Temporary Arrays” in SAS Functions and CALL Routines: Reference.
Multiple Variable Lists
The OF operator is also used to distinguish one variable list from another when multiple
ranged lists are used as arguments in functions. If more than one ranged variable list is
used, either the entire list is preceded by the OF operator and the lists are separated by
spaces or each list is preceded by the OF operator and the lists are separated by commas,
as shown in the following examples:
T=sum(OF x1-x3 y1-y3 z1-z3)
Or
T=sum(OF x1–x3, OF y1–y3, OF z1–z3)
For more information about the different types of variable lists used in SAS, see “SAS
Variable Lists” on page 49.
For a list of SAS functions that use the OF operator, see Table 11.1 on page 181.
The following table shows the types of SAS variable lists that are valid with the OF
operator:
SAS Variable Lists 51
..................Content has been hidden....................

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