Announcement

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

  • Creating New Variables

    Hi all.

    I'm working on a panel data set. I need to creat new variable(s) from existing variable(s). I'm not good at Stata beacuse i am beginner. I want to give an example what i want to do. I could not upload the image because of error, that's why i uploaded the image http://i57.tinypic.com/33o3y0y.png

    I want to create a new variable from variable fk070. It's age category varialbe, e.g. 11 = 35-39, 8=25-29, so on. hkimlik, fkimlik and fk010 are household id, individual id and year, repectively. New variable is dependency ratio which is a rate = (the number of persons who is <=14 and >=64)/total household member number. This variable must be created for each household and each year. How can i do this?

    Sincerely.

  • #2
    This is how I'd do it (although there are probably more efficient ways)
    ***1. Create dummies for dependents (1 if person is <=14 or >=64 and 0 otherwise) and person. I don't know your labels, let's say that less than 6 represents <=14 and above 15 represents >=64
    gen byte dep=(fk070<6 | fk070>15)
    gen person=1
    ***2. Sum all dependents and all persons who fall into the same household and year
    egen sum_dependents=sum(dep), by(hkimlik fk010)
    egen sum_all=sum(person), by(hkimlik fk010)
    ***3. Create the ratio
    gen ratio_dep=sum_dependents/sum_all

    Comment


    • #3
      I agree with the spirit of Juta's suggestion, but the dummies aren't needed, as you can sum the true-or-false expression directly.

      Code:
      egen dependents = total((fk070 < 6 | fk070 > 15) & fk070 < .), by(hkimlik fk010) 
      egen people = count(fk070), by(hkimlik fk010) 
      gen ratio = dependents/people
      should work fine. I am using total() not sum() as the egen function sum() went undocumented as of Stata 9, or so.

      I put in a condition to catch missings on fk070.



      Comment


      • #4
        Hi, goodmorning.

        Thank you much! Both worked!

        Sincerely.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          I agree with the spirit of Juta's suggestion, but the dummies aren't needed, as you can sum the true-or-false expression directly.

          Code:
          egen dependents = total((fk070 < 6 | fk070 > 15) & fk070 < .), by(hkimlik fk010)
          egen people = count(fk070), by(hkimlik fk010)
          gen ratio = dependents/people
          should work fine. I am using total() not sum() as the egen function sum() went undocumented as of Stata 9, or so.

          I put in a condition to catch missings on fk070.


          Dear Cox,

          You used "|" instead of "&". Can we use "or" operator for spesific ranges?

          Comment


          • #6
            For your information, "Cox" is my family name. I can't tell what your given name is as your identifier is too cryptic for me to decode, so let me remind you of the request that you use your full real name.

            I am clear that & (and) would make no sense where I used | (or). A variable can't be simultaneously less than 6 and more than 15.

            If I understand your question about "or" using specific ranges, then my code here is an example.

            Comment


            • #7
              Oh! Forgive me, i waste your time, you are right! Thx again! By the way,limonlusoda is just a nick name, soda water with lemon you can call me Omer.

              Comment


              • #8
                Please contact the administrators with a full real name as identifier. You can use the Contact us button on the home page.

                Comment

                Working...
                X