Announcement

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

  • Importing output from stata back into data

    I am using survey data. I want to assign the same value to each individual belonging to the same district after estimating it by using tabstat (since I need to give survey weight otherwise I know we can use bysort to do the same). Is there a way to use rclass or eclass command. If yes, how can we do that?

  • #2
    that depends on the details.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you for the reply Maarten.

      To elaborate on my problem:
      Eg. I have data at the individual level and I know the districts they belong to. The data is representative at the district level so I want to calculate the percentage of people employed in a district. And then insert back this estimated value back into my dataset as a new variable.

      I have generated a variable emp which takes values 1 for employed and 0 for unemployed individuals.
      now I am using the command:

      .tabstat emp [aw=wgt], by(district)

      this gives me the percentage of people employed in a district.

      Now I want to use this output generated by tabstat and somehow get these values back in my data as a new variable. so that for each individual we have a value of the percentage of people employed in the individual's district

      Comment


      • #4
        Were it not for the weights, this would be a job for -egen, mean()-. But you can still get this with a few -egen- commands:
        Code:
        by district, sort: egen numerator = total(wgt*emp)
        by district: egen denominator = total(cond(!missing(wgt, emp), wgt, .)
        gen wanted = numerator/denominator
        Note: Because no example data was provided, the above code is untested. It may contain typos or other errors or prove unsuitable for use in the actual data set. If further assistance is needed, be sure to give example data when posting back. Using the -dataex- command is the most helpful way to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        When asking for help with code, always show example data. When showing example data, always use -dataex-.


        Comment


        • #5
          Thank you so much Clyde. It worked perfectly.

          Just the parentheses were imbalanced.

          by district, sort: egen numerator = total(wgt*emp)
          by district: egen denominator = total(cond(!missing(wgt, emp)), wgt, .)
          gen wanted = numerator/denominator

          Comment


          • #6
            Thank you for posting the corrected solution.

            Comment

            Working...
            X