RETAIN Statement

使INPUT或赋值语句生成的变量在DATA step中的每一次迭代中保留其值。有效使用范围:DATA Step 。参考:Statements: RETAIN Statement - 9.2 (sas.com)

Without a RETAIN statement, SAS automatically sets variables that are assigned values by an INPUT or assignment statement to missing before each iteration of the DATA step. The RETAIN statement causes the values of all variables that are created with INPUT or assignment statements to be retained from one iteration of the DATA step to the next.

The RETAIN statement specifies variables whose values are not set to missing at the beginning of each iteration of the DATA step. RETAIN 针对的是在DATA步最一开始时没有被设定为缺失的变量。

初次之外,还可以用RETAIN设置初始值。You can also use RETAIN to assign an initial value other than the default value of 0 to a variable whose value is assigned by a sum statement.

 

Example

Create a variable seq2 that unique at studyid||usubjid||spid||stdtc level .

/*Create a variable seq2 that unique at studyid||usubjid||spid||stdtc level*/
data ds2;
set ds1;
    /*group data by studyid,usubjid,spid,stdtc*/
    by studyid usubjid spid stdtc; 
    /*Create variable seq and retain its value in each interation*/
    retain seq;   
    /*SAS identifies the beginning of a BY group by creating temporary variable first.var*/
    if first.usubjid then seq=1;   
    else seq=seq+1;
    /*Create variable eq2 and assign value to it*/
    seq2=seq;  
run;

 

posted on 2022-01-30 11:16  MOZY  阅读(67)  评论(0编辑  收藏  举报