Assignment Statement
Variable = expression
Examples of assignment values:
X = X1 + x2;
X = sum (OfX1-x6); To use
Sum (x, y); if either X or Y is a missing value, the sum result sets the missing value to 0, and if z = x + y is used; if there is a missing value, the result of Z will be the missing value.
AR (1) = Br (1); * assign the first element of the array Br to the first element of the array ar. The subscript of the array in SAS starts with 1;
X = x + y;
X
X = y = z * If y = z, x = 1; otherwise, 0 is used;
Type of the value assignment statement variable after calculation by expressionThere are three casesIs an executable statement
Numeric type = numeric type + numeric type
Character type = character type + character type
Numeric type = numeric type + numeric type (note: the next two locations can be exchanged, for example, x = "1" + 2. If the conversion fails, an error will be reported in the log window)
Rename statement
The keep statement is compiled before the rename statement. Both are non-executable statements.
Rename y = YY;
Keep x y;
The rename option must be enclosed in parentheses for all renamed variables.
Result Variable Length
The numeric length is 8;
The variable length is determined by the calculation result of the expression read for the first time.For example, char = "AB"; the char length is 3. No matter how long the PDV reads data, only two bytes are displayed.Previously, the length statement was used to define the char length.To solve this problem.
Length variable-specification (s) <default = n>;
Length m 5;
Length m 1 m2 7; * m1 and m2 are both 7;
Length M1 4 m2 6;
Lenght M1 3 m2 $8; specifies the second variable of the sequence type
The difference between a accumulate statement and a retain statement is an executable statement.
Variable + expression
For example, x + 1 x + (-1) cannot be written as X-1 x + (y = z)
In the compilation phase, PDV automatically sets the accumulate variable to 0. If you want it not to be 0, replace it with the retain statement. The accumulate statement can be considered as a special case of retain.
Keep, drop statement "unexecutable statement"
TIPS: (the difference between the keep option and the keep Statement)
Data A1; Set sashelp. class (keep = Name sex);/* keep name sex; */* if you use the keep statement instead of the keep option, then, PDV reads all the variables first and then retains the variables to be retained. With the keep = option, it can keep the variables to be read in each read-only entry;
Set sashelp. Class (keep = _ character _); * read-only entry to variable type;
Set sashelp. Class (keep = _ numeric _); * read-only numeric variables;
Run;
The usage of the drop statement is the same as that of the keep statement. In addition, the efficiency of the drop = option is higher than that of the drop statement.
Retain statement "unexecutable statement"
This is important. Let me explain the SAS operating mechanism first.
There is actually a loop between the data step and the run Statement of SAS. For example, when the set statement is used, an observation is set each time, and the observation is always run until run; then the start of the program is returned, continue to set the second observed value and read the observed values of the dataset.
Every time the system reads all the statements in the Data step, PDV sets all variables as missing values, which are represented by "." in SAS. Then, assign values to the variables when executing other statements. However, if we use the retain statement in the Data step
PDV will not clear the variable corresponding to the retain statement, but will keep it until the next change is executed, so that it can complete the accumulation of the initial value not 0 as mentioned above.
Data A1; Retain m1-m5 (1); * m1 is 1 other authority missing value; Retain m1-m5 1; * m1-m5 all is 1; Retain m1-m5 (, 8 ); * assign different values to each, and there is no difference below, need to be executed separately; Retain m1-m5 (1 2 3 4 5 );
Retain m1-m5 (1:5); Put m1-m5; run;
Libname chapt4 'e: \ SAS-data \ book_data \ chapt4 ';
Proc sort data = chapt4.retain1; by ID; run;
Data A1;
Set chapt4.retain1;
By ID;
Retain cns_sum id_cnt txn_cde_conditon txn_dte_min; * If retain txn_dte_min is omitted, the value of this parameter is missing for each loop;
If first. ID then do;
Cns_sum = 0;
Id_cnt = 0;
Txn_cde_conditon = 0;
Txn_dte_min = txn_dte;
End;
Put cns_sum =;
Cns_sum + CNS; * However, if you do not need to save the previous retain cns_sum id_cnt txn_cde_conditon, these values will not be wrong. If no value assignment statement is displayed, only the values are accumulated, these values will not be set as missing values in each round of loop, but will still have a retain effect;
Id_cnt + 1;
Txn_cde_conditon + (txn_cde in ("101" "201 "));
Put txn_dte_min = txn_dte =;
Txn_dte_min = min (txn_dte, txn_dte_min); * The retain statement cannot be discarded if there is a value assignment statement;
If last. ID;
Put "last. ID ";
Run;
Below is the dataset