0

I have a panel/longitudinal dataset in SAS.

One field indicates a class or type, another a point in time without breaks, another is the observed history and another is the log difference forecast for said history. I'd like to add a new field: the history field, advanced by the forecast field.

So if the time field is in the 'future', I want to recursively advance my goal variable with its own lag, multiplied by the exp of the log-difference forecast variable. A trivial operation it seems to me.

I've attempted to replicate the problem with a toy dataset below.

data in;
input class time hist forecast;
datalines;
1   1  100 .
1   2   .  .1
1   3   .  .15
1   4   .  .17
2   1  100 .
2   2   .  .18
2   3   .  .12
2   4   .  .05
run;

proc sort data=work.in;
by class time;
run;

data out;
    set in;
    by class time;
retain goal hist;
if time > 1 then goal= lag1(goal) * exp(forecast); 
run;
JPErwin
  • 223
  • 1
  • 10

1 Answers1

1

JP:

You might want this:

data out;
    set in;
    by class time;

    retain goal;

    if first.class 
      then goal=hist;
      else goal = goal * exp(forecast);
run;

Retaining a non data set variable can mostly be considered a lag1 type of stack. The initial goal needs to be reset at the start of each group.

Your first attempt is conditionally LAG1'ng a retained variable while BY group processing -- makes my head spin. LAG-n is tricky because the implicit LAG stack is updated only when processing flow goes through it. If a conditional bypasses the LAG function invocation there is no way the LAG stack can get updated. If you do see LAG in other SAS coding, it might appear in an unconditional place prior to any ifs.

NOTE: retaining data set variables (such as hist) is atypical because their values are overwritten when the SET statement is reached. The atypical case is when testing the retained data set variable prior to the SET statement has a functional purpose.

Richard
  • 25,390
  • 3
  • 25
  • 38
  • Note that there is no need to add a RETAIN statement for a variable that is sourced from an input dataset. Such variables are already retained. That is how MERGE statement is able to do a 1 to many merge. – Tom Nov 15 '17 at 02:28