Announcement

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

  • Foreach question

    Click image for larger version

Name:	Screen Shot 2018-04-11 at 14.58.38.png
Views:	1
Size:	15.6 KB
ID:	1438774

    Hi, I have the following data structure (although more data points). How can I instruct Stata to replace x with 1 for wave 2, if the data has the same id? Can I do it with foreach?

  • #2
    "If the data has the same id" is not meaningful to me. Perhaps you mean "if there is another observation that has the same id value?" I see nothing to make me think that -foreach- would be relevant, which is used to loop over values, variables, or the content of locals. The solution to your problem likely involves something like:
    Code:
    replace x = 1 if (wave == 2) & (some other condition involving "same id")

    Comment


    • #3
      Originally posted by Mike Lacy View Post
      "If the data has the same id" is not meaningful to me. Perhaps you mean "if there is another observation that has the same id value?" I see nothing to make me think that -foreach- would be relevant, which is used to loop over values, variables, or the content of locals. The solution to your problem likely involves something like:
      Code:
      replace x = 1 if (wave == 2) & (some other condition involving "same id")
      Apologies, yes, if the id value is the same! I want to replace x=1 if wave ==2 and id value in wave 2 is the same as id value in wave 1. Essentially copy the x from wave 1 to wave 2 for the same id.

      Comment


      • #4
        Originally posted by alex badalyan View Post

        Apologies, yes, if the id value is the same! I want to replace x=1 if wave ==2 and id value in wave 2 is the same as id value in wave 1. Essentially copy the x from wave 1 to wave 2 for the same id.
        Code:
        sort id wave
        replace x=1 if (wave==2 & id==id[_n-1] & wave[_n-1]==1)
        This mechanism is called explicit subscripting. See help subscripting.

        Note that this does not, as mentioned in your latter post, copy values from wave 1 to wave 2; it sets x to the value 1 for all observations from wave 2 that feature a wave 1 predecessor. In order to copy the earlier value, you need to use explicit subscripting in the replace instruction as well:
        Code:
        sort id wave
        replace x=x[_n-1] if (wave==2 & id==id[_n-1] & wave[_n-1]==1)
        There are more elegant ways to do this, quite likely involving declaring the data to be panel data with tsset, and using time series notation to insert lagged values, but you don't tell us enough about your data layout nor the result you try to achieve to give you more precise hints.

        Regards
        Bela
        Last edited by Daniel Bela; 11 Apr 2018, 08:12. Reason: added info about tsset

        Comment


        • #5
          Originally posted by Daniel Bela View Post

          Code:
          sort id wave
          replace x=1 if (wave==2 & id==id[_n-1] & wave[_n-1]==1)
          This mechanism is called explicit subscripting. See help subscripting.

          Note that this does not, as mentioned in your latter post, copy values from wave 1 to wave 2; it sets x to the value 1 for all observations from wave 2 that feature a wave 1 predecessor. In order to copy the earlier value, you need to use explicit subscripting in the replace instruction as well:
          Code:
          sort id wave
          replace x=x[_n-1] if (wave==2 & id==id[_n-1] & wave[_n-1]==1)
          There are more elegant ways to do this, quite likely involving declaring the data to be panel data with tsset, and using time series notation to insert lagged values, but you don't tell us enough about your data layout nor the result you try to achieve to give you more precise hints.

          Regards
          Bela
          Amazing, thank you! This was what I was looking for.

          Actually this is what it is for my case:
          Code:
           sort id wave
          
          replace x=1 if (wave==2 & id==id[_n-1] & x[_n-1]==1)

          Comment

          Working...
          X