Announcement

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

  • Replacing rest of the waves with same observations as in Wave 1 for some particular ID

    Hi guys,

    I have one quick question since I am little confused how to do it while dealing with the big dataset. I have generated a fake data here to put up my question. As you can see three variables in the example first one is id, the second one is wave and third which is the last variable is about insurance. As you can see here in wave 1 it is asked if somebody is insured as shown by 1 otherwise missing. Now here is my question. If an ID is insured in wave 1, I want to replace 1 in rest of the waves for that ID otherwise missing. Thanks for the help.

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id wave insurance)
    12 1 1
    12 2 .
    12 3 .
    13 1 .
    13 2 .
    13 3 .
    14 1 1
    14 2 .
    14 3 .
    15 1 .
    15 2 .
     5 3 .
    16 1 1
    16 2 .
    16 3 .
    17 1 .
    17 2 .
    17 3 .
    end

  • #2
    It seems you are dealing with (extreme) example of last observation carried forward. Well, in fact, it is the first one...

    That being said, you may use some data management commands , but the SSC carryfoward may be helpful to you.
    Best regards,

    Marcos

    Comment


    • #3
      Marcos Almeida Sorry i did not understand what you mean. The above mentioned dataset is an example. Yes i want to carry forward the observation in first wave for the rest of the waves for example in ID 14 you see 1 in insurance variable for wave 1. I want to carry forward 1 for the rest of the two waves for ID 14. Can you suggest me the command ?

      Comment


      • #4
        But Marcos Almeida did suggest a command: carryforward from SSC.

        Also, consider mipolate from SSC.

        To find out more, use search within this forum, or use help or search in Stata, as in

        Code:
        help ssc 
        
        search carryforward 
        
        search mipolate

        Comment


        • #5
          Sorry for writing again. I am trying carryforward but not working. I read from stata help carryforward but I am confused how to specify this issue. Already tried several ways but it did not work.

          Comment


          • #6
            Please read the FAQ. ‘Not working’ is a term to avoid, so uninformative it is.

            I just to wish to know whether you have followed the advice correctly:

            Have you found the SSC carryforward?
            Have you installed it?
            Have you typed - help carryforward?
            Have you seen examples with this command?
            Have you applied to your situation?

            If you answered ‘yes’ to all the questions, please type what you did as well as the error message.

            If you answered ‘no’ to one of the questions, please don’t proceed unless the respective step is fulfilled accordingly.

            Last edited by Marcos Almeida; 23 Jun 2018, 11:11.
            Best regards,

            Marcos

            Comment


            • #7
              I think we can do this using basic Stata commands. Perhaps the following starts you in a useful direction.
              Code:
              bysort id (wave): replace insurance = insurance[1] if wave>1 & wave[1]==1 & insurance[1]!=.
              Code:
              . list, noobs sepby(id)
              
                +----------------------+
                | id   wave   insura~e |
                |----------------------|
                |  5      3          . |
                |----------------------|
                | 12      1          1 |
                | 12      2          1 |
                | 12      3          1 |
                |----------------------|
                | 13      1          . |
                | 13      2          . |
                | 13      3          . |
                |----------------------|
                | 14      1          1 |
                | 14      2          1 |
                | 14      3          1 |
                |----------------------|
                | 15      1          . |
                | 15      2          . |
                |----------------------|
                | 16      1          1 |
                | 16      2          1 |
                | 16      3          1 |
                |----------------------|
                | 17      1          . |
                | 17      2          . |
                | 17      3          . |
                +----------------------+
              Added in edit: This crossed with Marco's good advice on presenting problems. Let me add the request that you please improve your subsequent posts by reviewing the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question.

              The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

              Comment


              • #8
                William gave you a command to type directly from Stata.

                Using the SSC carryforward, if you see the example, you can reach this by typing:

                Code:
                . by id (wave), sort: carryforward insurance, gen(my_fwd)
                my_fwd:  (6 real changes made)
                
                 
                . list, sepby(id)
                
                     +-------------------------------+
                     | id   wave   insura~e   my_fwd |
                     |-------------------------------|
                  1. |  5      3          .        . |
                     |-------------------------------|
                  2. | 12      1          1        1 |
                  3. | 12      2          .        1 |
                  4. | 12      3          .        1 |
                     |-------------------------------|
                  5. | 13      1          .        . |
                  6. | 13      2          .        . |
                  7. | 13      3          .        . |
                     |-------------------------------|
                  8. | 14      1          1        1 |
                  9. | 14      2          .        1 |
                 10. | 14      3          .        1 |
                     |-------------------------------|
                 11. | 15      1          .        . |
                 12. | 15      2          .        . |
                     |-------------------------------|
                 13. | 16      1          1        1 |
                 14. | 16      2          .        1 |
                 15. | 16      3          .        1 |
                     |-------------------------------|
                 16. | 17      1          .        . |
                 17. | 17      2          .        . |
                 18. | 17      3          .        . |
                     +-------------------------------+
                I recommend to create a new variable with the carry-forwarded information, as shown above.

                However, if you wish to change the values of the variable (I still advice against it, for the original data will be lost), you may type:

                Code:
                
                . by id (wave), sort: carryforward insurance, replace
                insurance:  (6 real changes made)
                
                . list, sepby(id)
                
                     +----------------------+
                     | id   wave   insura~e |
                     |----------------------|
                  1. |  5      3          . |
                     |----------------------|
                  2. | 12      1          1 |
                  3. | 12      2          1 |
                  4. | 12      3          1 |
                     |----------------------|
                  5. | 13      1          . |
                  6. | 13      2          . |
                  7. | 13      3          . |
                     |----------------------|
                  8. | 14      1          1 |
                  9. | 14      2          1 |
                 10. | 14      3          1 |
                     |----------------------|
                 11. | 15      1          . |
                 12. | 15      2          . |
                     |----------------------|
                 13. | 16      1          1 |
                 14. | 16      2          1 |
                 15. | 16      3          1 |
                     |----------------------|
                 16. | 17      1          . |
                 17. | 17      2          . |
                 18. | 17      3          . |
                     +----------------------+
                To end, I also believe that the id = 5 is a typo of yours. I guess it should be id =15, but I left your toy example unscathed for that matter.

                Hopefully that helps.
                Last edited by Marcos Almeida; 23 Jun 2018, 11:35.
                Best regards,

                Marcos

                Comment

                Working...
                X