Announcement

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

  • Create a variable that defines the value of the next row

    Hi,

    I have a variable called ia_label that defines an area of interest and a variable called rankIA that gives the order in which that area of interest was accessed by participants. I want to create a third variable that defines, for each area of interest and each participant, what was the next area of interest that was accessed. Example of data below:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int participant str11 ia_label float(rankIA nextIA)
    2 "Label 3" 3 .
    2 "Label 2" 2 .
    2 "Label 1" 1 .
    end
    An example of what I am aiming for is as follows:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int participant str11 ia_label float rankIA str7 nextIA
    2 "Label 3" 3 ""      
    2 "Label 2" 2 "Label 3"
    2 "Label 1" 1 "Label 2"
    end
    Any thoughts would be much appreciated.
    Last edited by Deirdre Robertson; 27 Mar 2019, 11:40.

  • #2
    There are some aspects of your question that are not really clear. I'll tell you what I assume about them. If my guesses are wrong, we have both wasted our time.

    I assume that in your real data, you have multiple participants, with distinct values of id, and that you do not want to use the first value of the next id as the "next row" for the last value of a given id. In assuming this I am not taking "next row" literally but imagining you have a more meaningful structure in mind.

    Also, evidently the results of any code that does this are going to depend on the sort order of the observations to begin with. In your data there is no obvious variable such as a date or time that suggests what the order should be. Rather than assuming such a variable exists in your data, I will assume that you have somehow managed to assure that your data is actually in the correct order already, and I will "reify" that order with a new variable. Just be aware that this means that if you reuse this code you need to be certain that you have the data in the correct sort order before doing so.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int participant str11 ia_label float rankIA
    2 "Label 3" 3
    2 "Label 2" 2
    2 "Label 1" 1
    end
    
    gen long obs_no = _n
    by participant (obs_no), sort: gen nextIA = ia_label[_n-1]
    Note that I have revised your original -dataex- output to omit the variable nextIA, which the subsequent code creates.

    Also, I find it strange that you are calling this variable nextIA, because in the ordinary sense of the words, it is really the previous value of ia_label.

    Comment


    • #3
      Hi Clyde,

      Thanks very much for your reply. That works for me. Yes I was calling it nextIA because I had set about it trying to assign the value of the subsequent row but it works equally well to assign the previous row instead, I'll just rename the variable.

      Appreciate your help.

      Best wishes,
      Deirdre

      Comment

      Working...
      X