sas中retain语句的作用

整理自: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
-----------------------------------------


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

  • 8
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值