sas中retain语句的作用

来源:互联网 发布:javiewer差不多的软件 编辑:程序博客网 时间:2024/05/17 02:38

整理自:http://bbs.pinggu.org/thread-952476-1-1.html


Q1:
data a; 
      set b;
run;     

Set的作用是将数据集b中的记录逐条读入PDV中,在run语句的地方SAS会将pdv中的变量输出到数据集a中。
SAS帮助:
What SET DoesEach time the SET statement is executed, SAS reads one observation into the program data vector. SET reads all variables and all observations from the input data sets unless you tell SAS to do otherwise. A SET statement can contain multiple data sets; a DATA step can contain multiple SET statements. 


Q2:
RETAIN的作用:The RETAIN statement prevents SAS from re-initializing the values of new variables at the top of the DATA step.
Previous values of retained variables are available for processing across iterations of the DATA step.

对于数据集中新声明的变量,SAS会在data步循环执行开始时将其置为空值,而如果该变量是retain的变量,则不被置空。

还是看个例子:

数据集aaa,有三条记录。
data aaa;
     do x=1,3,5;
     output;
end;
run;

数据集bbb新声明变量y,在读入aaa的第一条记录时将y值设为1. 这样在第二条和第三条记录中y的值为空。

data bbb;
    set aaa;
    if _n_=1 then y=1;
    put _all_;
run;

log:
-----------------------------------------------
x=1 y=1 _ERROR_=0 _N_=1
x=3 y=. _ERROR_=0 _N_=2
x=5 y=. _ERROR_=0 _N_=3
-----------------------------------------------


data步ccc将y声明为retain的变量,在读第一条记录时令y=1,此值会被保留到第二条和第三条记录中。

data ccc;
     set aaa;
     retain y;
  if _n_=1 then y=1;
  put _all_;
run;

log
------------------------------------------
x=1 y=1 _ERROR_=0 _N_=1
x=3 y=1 _ERROR_=0 _N_=2
x=5 y=1 _ERROR_=0 _N_=3
-----------------------------------------


表达比较乱,希望对你有点用处。

0 0
原创粉丝点击