Reputation: 55
I have a time series data set of over 3,000 observations. I am trying to create a new variable that is dependent on that variable's previous value and I am having trouble getting SAS to calculate each observation in order to ensure that the previous value is calculated before it gets to the next observation.
I have set the first observation to 1. Then for the remaining observations I want SAS to calculate:
New_var = lag(new_var) * (1 + Var2)
Any ideas? I am sure it is a relatively easy answer, but I haven't been able to decipher it myself.
Upvotes: 1
Views: 124
Reputation: 333
I had the same question and reached here searching for it. I came out with the following logic and so thought of answering it here as well for future SAS enthusiasts:
Use (or create) an existing (or new) variable which has unique values. You may use retain statement in SAS or monotonic() function in proc sql as shown below:
/using proc sql/ proc sql; create table temp6 as select a.*, monotonic() as var3 from temp5 as a; quit;
/using data step/ data temp7; set temp5; retain var3; var3 + 1; run;
Once you have this variable, you may use the retain statement and first. flag to calculate New_Var as below. I have initialized the New_Var with 1 for the very first row. You may change the initial value accordingly.
data temp8; set temp7; by var3; retain New_Var 1; if first.var3 then New_Var = New_Var * (1 + Var2); run;
Hope this helps!!
Upvotes: 0
Reputation: 481
You could try a retain statement instead of lag.
retain new_var;
new_var = new_var * (1+Var2);
Upvotes: 3