Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cascading values when producing a new variable which is a combination of several other variables

    My data resembles the following:


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(year y x1 new)
    1990 23 1.5 .
    1991  . 2.1 .
    1992  . 2.1 .
    1993  . 4.6 .
    end
    Here, I have a dataset (which I have declared as time series for convenience), and for which I have variables y, x1 and a new variable. The first value of the new variable is to be computed as the value of y in the first year (say 1990) times the value of x1 in the following year. This is straightforward:


    Code:
    replace new=y*F.x1 if year==1990
    However, for every following year for new, I wish to use the previous value of new multiplied with the current value of x1. I run:

    Code:
    replace new=new[_n]*x1[_n+1] if year>1990
    or

    Code:
    replace new=new*F.x1if year>1990
    This works for only the first observation after 1990, i.e. 1991. In other words, values do not cascade down to use new non-missing values of the new variable. Is there something obvious I am missing?

    Thanks,
    CS

  • #2
    Code:
    replace new=y*F.x1 if year==1990
    replace new = L1.new*x1 if year > 1990

    Comment


    • #3
      Thank you so much. Is this because cascading only works when the variable that is to be computed is itself used with timeseries operators? Does cascading not work across (transformations) of variables?

      Comment


      • #4
        Cascading works with any variable.

        The problem with your original code is that in the year > 1990 part you were referencing the current (_n) and future (_n+1, F) values, whereas you need to reference the past (L, or _n-1) and current values, respectively.

        Comment

        Working...
        X