Search code examples
loopslagspss

SPSS Identifying Different Lagged Values Through Loops


I have this dataset with 2 variables: week and brand_chosen, where brand chosen designates which product from e.g. a super market was chosen, an it looks like this.

Week     brand_chosen
 2           19
 2           15    
 2           50
 2           12
 3           19
 3           16
 3           50
 4           77
 4           19

What I am trying to do is for each line, to note the week in which the brand purchase was made, and check if in the week before that the same brand purchase was made. In case it did, a variable dummy would take the value of 1, otherwise 0.
Because week appears multiple times I cannot take just the lag(week,1), so I probably need to loop through the week variables for each case, until it finds the first different value.

This is what i tried to do

loop i=1 to 70.  
   do if (week<>lag(week,i) and brand_chosen=lag(brand_chosen,i)).  
      compute dummy=1.  
   end loop.  
   else.  
      compute dummy=0.  
   end if.  
end loop.  
execute.

Where 70 is just an arbitrary number so that I am sure that it will check all the previous cases. I get two problems with that. First the lag function needs to contain a number from what I understand but "i" is not considered a number here.
The second problem is that i would like to close the loop if the condition is satisfied, and move to the next case but I get an error.

I am new to spss syntax and I am struggling with that one, so any help is greatly appreciated.


Solution

  • I assume that every combination of week--brand_chosen is unique. In this case the solution is quite simple. Just reorder your dataset by brand_chosen and then week, and then run a simple lag command.

    This should do the trick:

    SORT CASES BY brand_chosen week.
    COMPUTE dummy=0.
    IF (brand_chosen=LAG(brand_chosen) AND week>LAG(week)) dummy = 1.