Announcement

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

  • Question about if command

    Hello. I have cross-national time-series dataset from 1990 to 2016

    I am studying the effect of a decreasing democracy score, and i have a question as i want to create a binary variable that gives a value of 1 if the democracy variable is statistically lower than the year before, and 0 if not, so that i can use an if command to only observe drops in democracy and not increases.

    So if for example the variable has a value of 7 in year 1999 and a value of 5 in 2000, the binary variable would then give me a 1.

    Furthermore, to check and see how much effect the drop has i have tried to make a variable that is simply the difference of said democracy variable from year to year+1. I tried using a command such as

    gen diff = var-(var_n-1]) but that messed it up when it changes countries in the variables

    Thank you

  • #2
    Two of various possible ways:

    1. works fine without gaps

    Code:
    bysort id (time) : gen diff = var - var[_n-1]
    the right-hand side being what I guess you intended to write.

    2. works even with gaps

    Code:
     
    tsset id time 
    gen diff = D.var
    See also

    Code:
    help varlist
    Further, watch out: you need the if qualifier, not the if command.

    Comment


    • #3
      Thank you very much that solved it, and i managed to figure the other issue out once that was solved. Thaaank you.

      Comment

      Working...
      X