Ask Your Question
2

What is a quicker method to generate a variable using previously assigned values in SAS programming with the retain statement?

asked 2021-05-07 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-05-08 07:00:00 +0000

woof gravatar image

One quicker method to generate a variable using previously assigned values in SAS programming with the retain statement is to use the LAG function. The LAG function allows you to retrieve the value of a variable from the previous observation. Here's an example:

data test;
   retain prev_var 0;
   set input_data;
   new_var = prev_var + var;
   prev_var = new_var;
run;

In this example, we're creating a new variable called "newvar" by adding the current value of "var" to the previous value of "prevvar". We use the LAG function to retrieve the previous value of "prevvar". This eliminates the need for a separate RETAIN statement for "newvar" and reduces the amount of code needed.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-07 11:00:00 +0000

Seen: 9 times

Last updated: May 08 '22