Help using Base SAS procedures

add previous Observation to variable

Reply
N/A
Posts: 0

add previous Observation to variable

Hi all,

I want to make a training curve. Therefore I must add the previous Observation to my variable "X".
OBServation1: X= Condition from OBS1
OBServation2: X= x from OBS1 plus Condition from OBS2
OBServation3: X= x from OBS2 plus Condition from OBS3
OBServation4: X= x from OBS3 plus Condition from OBS4
OBServation5: X= x from OBS4 plus Condition from OBS5
OBServation6: X= x from OBS5 plus Condition from OBS6

But I dont know how I get the previous Observation. Maybe have anybody an idea?

Obs Condition X
1 0 0
2 1 1
3 1 2
4 0 2
5 0 2
6 1 3

Thanks, Lex
SAS Employee
Posts: 174

Re: add previous Observation to variable

Posted in reply to deleted_user
data input;
input obs value;
datalines;
1 0
2 1
3 1
4 0
5 0
6 1
;
run;

data x;
set input;
retain x1;
x1 + value;
run;

Or you could experiment with the LAG function - http://support.sas.com/onlinedoc/913/getDoc/en/lrdict.hlp/a000212547.htm
Super Contributor
Posts: 474

Re: add previous Observation to variable

Posted in reply to deleted_user
If I got this right, you should RETAIN variable X.

Retain, holds the value of the previous iteration.

Assuming that table1 holds the input data.

data table2;
set table1;
retain X 0; /* explicit retain for the X variable */
X=X+Condition;
run;

or simply:

data table2;
set table1;
X+Condition; /* implicit retain for the X variable, when using RETAIN_VAR+ */
run;

Both are the same.
On the latest, there's no need to explicitly use the retain statement.

Greetings from Portugal.

Daniel Santos at www.cgd.pt
N/A
Posts: 0

Re: add previous Observation to variable

Posted in reply to DanielSantos
Thanks. This is what Im looking for.

Greetings

Lex
Ask a Question
Discussion stats
  • 3 replies
  • 112 views
  • 0 likes
  • 3 in conversation