Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.