Announcement

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

  • Creating a new variable based on all the following values of an existing variable

    Hello -- I was unsure how to word the title, but hopefully it offers some clue as to what I am trying to do. I have the following dataset:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float year str8(k i)
    1996 "DEU" "ARG"
    1996 "DEU" "AUS"
    1996 "DEU" "AUT"
    1996 "DEU" "BEL"
    1996 "DEU" "BGR"
    1996 "USA" "ARG"
    1996 "USA" "AUS"
    1996 "USA" "AUT"
    1996 "USA" "BEL"
    1996 "USA" "BGR"
    end
    I would like to create a new variable "j" which would would allow each possible i, j pair to be represented, with respect to k. I.e. k = DEU, i = ARG, j = AUS would be the first observation, then k = DEU, i = ARG, j = AUT would be next, and so on. Once all the i, j pairs are created for DEU, the i, j pairs would then be created for USA. My initial (naive) attempt at this was:
    Code:
    gen j = i[_n+1]
    , but of course that does not create all of the pairs for each i country that I needed. Thank you for your help.

  • #2
    I can't say I feel confident I understand what you want. But is it, perhaps, this?
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float year str8(k i)
    1996 "DEU" "ARG"
    1996 "DEU" "AUS"
    1996 "DEU" "AUT"
    1996 "DEU" "BEL"
    1996 "DEU" "BGR"
    1996 "USA" "ARG"
    1996 "USA" "AUS"
    1996 "USA" "AUT"
    1996 "USA" "BEL"
    1996 "USA" "BGR"
    end
    
    preserve
    tempfile copy
    rename i j
    save `copy'
    
    restore
    joinby year k using `copy'
    drop if i >= j
    
    sort year k i j

    Comment


    • #3
      Clyde, sorry for not being quite clear; but indeed, the "joinby" command was the missing piece that I needed. Thank you!

      Comment

      Working...
      X