Announcement

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

  • Create a loop to calculate a 1-0-Dummy

    Hi,

    I have the following issure: Attached is a png-file with three variables:
    In the first column you can see the year, in the second column is the number of an individual and in the third is a dummy variable whether the individual had an accident in one of the years or not.
    You can see, that each number of an individual appears 4 times, consistent to the number of different years. Now I want to generate a dummy that takes the value 1 if the individual had at least one accident in the four years, zero otherwise.
    This would be of great help to me.
    If you need further information, feel free to tell me.

    Cheers

  • #2
    Look at the help for egen. Do something like

    Code:
    egen xaccident = max(accident), by(id)
    Also look at the xt (panel data) commands if you aren't familiar with them already.
    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    StataNow Version: 18.5 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      This is a nice article by Nick Cox for problems like this (and more complicated ones):

      http://www.stata-journal.com/sjpdf.h...iclenum=dm0033
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 18.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        perfect answer, that's what I was looking for.
        Thany you very much

        Comment


        • #5
          I have one last question.
          Enclosed is another screenshot of the data editor.
          In the third column you can see the variable "type_of_coverage"
          Now I want to generate a dummy that takes the value 1, if the type_of_coverage is the same for all four years (2008 - 2011) for one individual. How do I implement this?
          I am reading through your suggested article meanwhile. Nevertheless I would be very grateful if you can help me with this problem
          Thanks in advance

          Comment


          • #6
            This is an FAQ. See http://www.stata.com/support/faqs/da...ions-in-group/

            Code:
            bysort id (type_of_coverage) : gen is_same = type_of_coverage[1] == type_of_coverage[_N]
            Please see FAQ Advice on checking FAQs, using CODE mark-up rather than photos to show data in listing, and our preference for full real names, as previously requested of you (in thread http://www.statalist.org/forums/foru...mmjjjj-to-jjjj)
            Last edited by Nick Cox; 19 Jul 2014, 08:16.

            Comment


            • #7

              you are absolutely right, I should have checked the FAQ.
              Nevertheless thanks a lot for your patience!
              Appeciate it

              Comment

              Working...
              X