Announcement

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

  • Creating a variable with data from other variables respecting conditions

    Hi, i just started using stata (this is my first project) and i don´t know how to solve the following issue.

    With a Census data, i have a column ("A") with household control numbers (same numbers=same family), another column ("B") that informs me if the observation is from the owner of the house or his/her children/husband/wife. What i need is to create a column ("D") that replicate for all members in the house the values found in the third column ("C") of the owner. Something like this:

    A B C D
    1 Owner 1 1
    1 other 2 1
    1 other 3 1
    1 other 4 1
    2 owner 5 5
    2 other 6 5
    2 other 7 5
    3 owner 8 8
    3 other 9 8
    3 other 10 8

    Any ideas?

  • #2
    First, looking just at your sample data, sometimes you spell Owner and sometimes owner. Stata will not recognize those as the same. If your real data exhibit the same inconsistency (as is commonly the case for string variables in data sets) , or other common inconsistencies such as leading or trailing blanks, or multiple consecutive spaces within, you should, before doing anything else with the variable, clean it up with
    Code:
    replace B = trim(itrim(lower(B)))
    And even after that, you should tabulate it to see if there are other values that represent typos or spelling errors and then fix those.
    After all that, if you are quite sure that each household has at most one observation designated "Owner" in variable B, then the following will work:

    Code:
    by A, sort: egen D = mean(C/(B=="owner"))

    If you are just getting started with Stata, you should acquaint yourself with -by- and with -egen- in the manuals. They are indispensable to data management.

    Comment


    • #3
      Something like

      Code:
      egen C_owner = total(C / (B == "owner")), by(A)
      although the code will differ if B is numeric with value labels. Note that it must be "owner" exactly, or you need different code.

      See http://www.stata-journal.com/article...article=dm0055

      What about joint owners?

      Please see Statalist FAQ Advice Section 6.

      Comment


      • #4
        Thak you for your responses Mr. Schechter and Mr.Cox. In fact, my example was a ilustration for my problem, but with some other simple steps i could replicate with succes Mr. Cox solution. I´ll proceed to read the indicated articles and suggestions.

        Comment

        Working...
        X