Announcement

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

  • Adding new observations with specific values for some variables and the rest as duplicates

    Hi

    I have some US census data including information about individuals; where they live (state), housing characteristics, age, income etc.

    Currently there is one observation for each individual, I want to add 50 observations to each individual (so that there is 51 per individual), 1 observation per individual for every US state. I want these new observations to go directly under the first observation for each person.

    All the variables should be duplicates of the original observation but the variable that indicates the state (which should go from 1-51). And then I need a 'choice variable' that indicates where the individual is actually living (1 if individual lives in that state, 0 otherwise).

    I would be very grateful for any advice, I have tried to look at commands like expand and insobs but not really getting anywhere.

    Kind regards

    Stella

  • #2
    Welcome to Statalist, Stella.

    Lacking a sample of your data, the following code is not tested. It assumes the variable id is your individual identifier and the variable state is their actual state.
    Code:
    expand 51
    bysort id: generate newstate = _n
    generate choice = state==newstate
    To improve your future posts, please review 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, looking especially at sections 9-12 on how to best pose your question. In particular, please read FAQ #12 and help those whose help you seek by posting example data using the dataex command. If you are running Stata 15.1 or later, it is already installed. For earlier versions of Stata, install dataex by typing ssc install dataex. Type help dataex to read the simple instructions for using it. Using dataex will enable those who want to help you to quickly and easily create a 100% faithful replica of your situation to test their ideas and code on.

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

    Comment


    • #3
      Thank you, great answer. I will review the Statalist FAQs and Advice on Posting for future posts.

      Comment

      Working...
      X