Announcement

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

  • very basic question

    I’m sorry to bother the list with a rather basic question, and though I am sure it's been answered, I couldn’t find an answer on the list or in the Stata FAQs.

    My data look like this:

    id poor70 poor71 poor72 poor73 poor74 observns |
    |-----------------------------------------------------------------------|
    1. | 1 0 0 0 0 1 1 |
    2. | 2 0 0 1 1 . 1 |
    3. | 2 0 0 1 1 . 2 |
    4. | 3 1 1 . . . 1 |
    5. | 3 1 1 . . . 2 |
    |-----------------------------------------------------------------------|
    6. | 4 1 1 1 1 0 1 |
    7. | 4 1 1 1 1 0 2 |
    8. | 4 1 1 1 1 0 3 |
    9. | 4 1 1 1 1 0 4 |
    10. | 5 0 1 1 0 . 1 |
    |-----------------------------------------------------------------------|
    11. | 5 0 1 1 0 . 2 |

    Thus, I have 1 observation for each person for each year she is poor. What I would like to do is tell stata to treat each observation as a separate year for those the person is poor. So for person 1, for 1974, for person 2, one for 1973, one for 1974, etc. I would do this through do loops in SAS, but I am unsure how to do so in Stata. Any help is much appreciated.

    Best,

    Brendan

  • #2
    I can't understand what you want your final data set to look like, from the sentences, "What I would like to do is tell stata to treat each observation as a separate year for those the person is poor. So for person 1, for 1974, for person 2, one for 1973, one for 1974, etc.". Please provide a small toy example.

    Comment


    • #3
      Brendan, see help for reshape.

      Comment


      • #4
        Brendan,

        I agree with Dave and would also suggest possibly showing how you would solve this problem in SAS so that we can suggest a similar Stata solution.

        In the meantime, here is my best guess as to what you want:

        Code:
        gen yearpoor=.
        gen npoor=0
        forvalues yr=70/74 {
          replace npoor=npoor+1 if poor`yr'==1
          replace yearpoor=`yr' if poor`yr'==1 & observns==npoor
        }
        This assumes that the observations for a person are in chronological order. If that is not the case or if that is irrelevant, then you will need to explain more.

        Regards,
        Joe

        Comment


        • #5
          If that is what is wanted then do something like Sergiy says:

          Code:
          clear
          input id    poor70    poor71    poor72    poor73    poor74 observations
          1    0    0    0    0    1    1
          2    0    0    1    1    .    1
          2    0    0    1    1    .    2
          3    1    1    .    .    .    1
          3    1    1    .    .    .    2
          4    1    1    1    1    0    1
          4    1    1    1    1    0    2
          4    1    1    1    1    0    3
          4    1    1    1    1    0    4
          5    0    1    1    0    .    1
          5    0    1    1    0    .    2
          end
          
          duplicates report id poor*
          duplicates drop id poor*, force
          drop observations
          reshape long poor, i(id) j(year)
          bysort id: egen observations = total(poor)
          
          list, sepby(id)

          Comment


          • #6
            Thanks, all. I'm sorry for being unclear. I absolutely was. Reshape is almost certainly what I need. Cheers.

            Comment


            • #7
              Brendan,

              Please provide more informative titles for future questions.
              You should:

              1. Read the FAQ carefully.

              2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

              3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

              4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

              Comment

              Working...
              X