保留变量&初始化0 SAS-用例情况
I am very new to SAS & request help to understand use case of 'Retain' function, below are 2 codes where end goal is cumulative height to be added in new column 'Tot_Height', using 2 different codes, however both give same result, hence confused when would retain function be used and variable be initialised as 0 ?????
Data Set [Data Set][1] : (https://i.sstatic.net/nt1j6.png)
Code No 1
```
data class3;
set class2;
retain tot_height 0;
by sex;
if first.sex then tot_height = Height;
else tot_height + Height;
run;
````
Code No 2
````
data class3;
set class2;
if first.sex then tot_height = Height;
else tot_height + Height;
run;
````
请从上面的2个代码中了解,因为我感到困惑
I am very new to SAS & request help to understand use case of 'Retain' function, below are 2 codes where end goal is cumulative height to be added in new column 'Tot_Height', using 2 different codes, however both give same result, hence confused when would retain function be used and variable be initialised as 0 ?????
Data Set [Data Set][1] : (https://i.sstatic.net/nt1j6.png)
Code No 1
```
data class3;
set class2;
retain tot_height 0;
by sex;
if first.sex then tot_height = Height;
else tot_height + Height;
run;
````
Code No 2
````
data class3;
set class2;
if first.sex then tot_height = Height;
else tot_height + Height;
run;
````
Please help understand from above 2 codes as i am confused where would retain help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两个步骤之间没有真正的区别,因为在其他子句中使用了sum语句。
SUM语句的形式:
结果是表达式值添加到变量的值中。并且该变量被定义为保留,如果没有其他保留语句具有明确的初始值,则将其初始化为零。因此,在将初始值设置为零的第一个数据步骤中添加说明的保留语句不会改变数据步骤的工作方式。
通常,在此步骤创建的数据步骤变量的每次迭代中(与来自输入数据集的变量相反)将设置为丢失。保留声明说不要让它们失踪,因此迭代开始时的值与上一次迭代结束时相同。
请注意,对于这些数据步骤,初始值立即被第一个观察值的第一个高度值覆盖,因为根据定义,数据集中的第一个观察结果是对A组的第一个观察结果。因此,即使您要将保留语句中的初始值更改为零以外的东西,也不会立即更换它,也不会具有实际效果。
There is no real difference between those two steps because of the use of the SUM statement in the ELSE clause.
The SUM statement has the form:
The result is that the value of EXPRESSION is added to the value of VARIABLE. And that VARIABLE is defined to be RETAINed and if there is no other RETAIN statement with an explicit initial value it is initialized to zero. So adding the explicate RETAIN statement in the first data step that is setting the initial value to zero does not change how the data step will work.
Normally on each iteration of the data step variables that are created by this step (as opposed to variables that are coming from input datasets) will be set to missing. The RETAIN statement says to NOT set them to missing so the value at the start of on iteration is the same as it had at the end of the previous iteration.
Note that for these data steps the initial value is immediately overwritten by the first value of HEIGHT for the first observation since by definition the first observation in the dataset is the first observation of a BY group. So even if you were to change the initial value in the RETAIN statement to something other than zero it will have no real effect since it will be immediately replaced.