Chapter Contents |
Previous |
Next |
Ways to Create Variables |
You can create variables in a DATA step in the following ways:
Note: You can also create variables
with the FGET function. See SAS Language Reference: Dictionary for more information.
Using an Assignment Statement |
When the type and length of a variable are not explicitly set, SAS gives the variable a default type and length as shown in the examples in the following table.
Expression | Example | Resulting Type of X | Resulting Length of X | Explanation |
---|---|---|---|---|
Numeric variable | length a
4;
|
Numeric variable | 8 | Default numeric length (8 bytes unless otherwise specified) |
Character variable | length a
$ 4;
|
Character variable | 4 | Length of source variable |
Character literal | x='ABC';
|
Character variable | 3 | Length of first literal encountered |
Concatenation of variables | length a
$ 4
|
Character variable | 12 | Sum of the lengths of all variables |
Concatenation of variables and literal | length a
$ 4;
|
Character variable | 7 | Sum of the lengths of variables and literals encountered in first assignment statement |
If a variable appears for the first time on the right side of an assignment statement, SAS assumes that it is a numeric variable and that its value is missing. If no later statement gives it a value, SAS prints a note in the log that the variable is uninitialized.
Note: A RETAIN statement initializes a variable and can assign it an initial value,
even if the RETAIN statement appears after the assignment statement.
Reading Data with the INPUT Statement in a DATA Step |
The following example uses simple list input to create a SAS data set named GEMS and defines four variables based on the data provided:
data gems; input Name $ Color $ Carats Owner $; datalines; emerald green 1 smith sapphire blue 2 johnson ruby red 1 clark ;
Specifying a New Variable in a FORMAT or an INFORMAT Statement |
data sales; Sale_Price=49.99; format Sale_Price 6.2; run;SAS creates a numeric variable with the name Sale_Price and a length of 8.
See SAS Language Reference: Dictionary for more information about using the FORMAT and INFORMAT statements.
Specifying a New Variable in a LENGTH Statement |
data sales; length Salesperson $20; run;
For character variables, you must allow for the longest possible value in the first statement that uses the variable, because you cannot change the length with a subsequent LENGTH statement within the same DATA step. The maximum length of any character variable in the SAS System is 32,767 bytes. For numeric variables, you can change the length of the variable by using a subsequent LENGTH statement.
When SAS assigns a value to a character variable, it pads the value with blanks or truncates the value on the right side, if necessary, to make it match the length of the target variable. Consider the following statements:
length address1 address2 address3 $ 200; address3=address1||address2;
Because the length of ADDRESS3 is 200 bytes, only the first 200 bytes of the concatenation (the value of ADDRESS1) are assigned to ADDRESS3. You might be able to avoid this problem by using the TRIM function to remove trailing blanks from ADDRESS1 before performing the concatenation, as follows:
address3=trim(address1)||address2;
See SAS Language Reference: Dictionary for more information about using the LENGTH statement.
Specifying a New Variable in an ATTRIB Statement |
data lollipops; Flavor="Cherry"; attrib Flavor format=$10.; run;
Note: You cannot create a new variable by using a LABEL
statement or the ATTRIB statement's LABEL= attribute by itself; labels can
only be applied to existing variables.
See SAS Language Reference: Dictionary for more information about using the ATTRIB statement.
Using the IN= Data Set Option |
The following example shows a merge of the OLD and NEW data sets where the IN= option is used to create a variable named X that indicates whether the NEW data set contributed data to the observation:
data master missing; merge old new(in=x); by id; if x=0 then output missing; else output master; run;
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.