Note: This list is not exhaustive. For example, the SET, MERGE, MODIFY, and UPDATE statements can also create variables.
|Using an Assignment Statement|
In a DATA step, you can create a new variable and assign it a value by using it for the first time on the left side of an assignment statement. SAS determines the length of a variable from its first occurrence in the DATA step. The new variable gets the same type and length as the expression on the right side of the assignment statement.
|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 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|
When you read raw data in SAS by using an INPUT statement, you define variables based on positions in the raw data. You can use one of the following methods with the INPUT statement to provide information to SAS about how the raw data is organized:SAS Language Reference: Dictionary for more information about using each method.
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|
You can create a variable and specify its format or informat with a FORMAT or an INFORMAT statement. For example, the following FORMAT statement creates a variable named SALE_PRICE with a format of 6.2 in a new data set named SALES:
SAS creates a numeric variable with the name SALE_PRICE and a length of 8.
|Specifying a New Variable in a LENGTH Statement|
For character variables, you must use 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 SAS 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:
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:
|Specifying a New Variable in an ATTRIB Statement|
Note: You cannot create a new variable by using a LABEL statement or the ATTRIB statement's LABEL= attribute by itself; labels can be applied only to existing variables.
|Using the IN= Data Set Option|
The IN= data set option creates a special Boolean variable that indicates whether the data set contributed data to the current observation. The variable has a value of 1 when true, and a value of 0 when false. You can use IN= on the SET, MERGE, and UPDATE statements in a DATA step.
data master missing; merge old new(in=x); by id; if x=0 then output missing; else output master; run;