sasretain用法


2023年12月27日发(作者:苹果ios系统下载)

sasretain用法

The basic syntax of RETAIN statement is as follows:

RETAIN ;

Where,

are the variables

whose values have to be maintained through out the iteration of

the Data Step.

When the RETAIN statement is used, the value of the variable

will be maintained between the iterations until the variable is

modified with any arithmetic or character operation or until a

DROP or KEEP statement is encountered.

If the RETAIN statement is used and the OUTPUT statement is

used in the same DATA Step, then the output dataset will contain

observation for the variables other than the ones specified in

the RETAIN statement. For example, if the two variables x and y

are listed in the RETAIN statement, then the variables x and y

will be maintained from the previous iteration but the variables

other than x and y will be replaced from the current iteration.

Example 1:

Suppose we have the following dataset with four observations

and three variables

Data test1;

Input Var1 Var2 Var3;

Datalines;

1510

2720

3625

4930

Run;

Now if we want to add 10 to each observation of Var3 using a

DATA Step with a RETAIN statement, then the program will be as

follows

Data test2;

Set test1;

Retain Var3;

Var3 = Var3 + 10;

Run;

The resulting dataset will be as follows

Var1 Var2 Var3

1520

2730

3635

4940

Example 2:

Suppose we have the following dataset with four observations

and three variables

Data test3;

Input Var1 Var2 Var3;

Datalines;

1510

2720

3625

4930

Run;

Now we want to find the cumulative sum for each observation

in Var3. For this purpose, we will use a DATA Step with a RETAIN

statement.

Data test4;

Set test3;

Retain Var3;

Var3 = Var3 + Var3_old;

Run;

The resulting output dataset will be as follows

Var1 Var2 Var3

1510

2730

3655

4985

We can see that the value of Var3 in the second observation

is the sum of the Var3 values of the first and the second

observation and so on.

Example 3:

Suppose we have the following dataset with four observations

and three variables

Data test5;

Input Var1 Var2 Var3;

Datalines;

1510

27.

3625

4930

Run;

Now we want to assign the value of 10 to Var3 in the second

observation. For this purpose, we will use a DATA Step with a

RETAIN statement.

Data test6;

Set test5;

Retain Var3;

If Var3_old = . Then Var3 = 10;

Run;

The resulting output dataset will be as follows

Var1 Var2 Var3

1510

2710

3625

4930

We can see that the value of Var3 in the second observation

is assigned the value of 10. This is done using the RETAIN

statement.

From the above examples, we can clearly see that the SAS

RETAIN statement is used to maintain the current value of a

variable through multiple iterations of a DATA step. It is

mainly used when we have to manipulate the data like calculating

a cumulative sum or finding the difference between the values of

the same variable at different iteration or to assign a missing

value when the first non-missing observation of the variable is

encountered.


本文发布于:2024-09-24 08:22:48,感谢您对本站的认可!

本文链接:https://www.17tex.com/fanyi/39051.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:苹果   官网   系统   下载
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议