Announcement

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

  • stata

    Hello
    I have the household code, the ratio of household members to the guardian and the age of the household members
    How do I calculate the following variables with the STATA
    - Number of children under the age of 15 years in household
    - Number of people over 15 years old in household
    Thank you

  • #2
    Hi fereshte,

    It would be helpful if you could post 20-30 obs of your data using dataex (there's a video tutorial here.) It's not clear whether your data are in wide or long format. For the data example below, I assumed it was in long format.

    Also, in the future it would be helpful if you provided a more descriptive title for your post.

    Code:
    * I created some toy data
    dataex hhid age  // data shared via -dataex-. To install: ssc install dataex
    clear
    input byte(hhid age)
    1 64
    1 54
    1 22
    1 19
    1 13
    2 51
    2 41
    2 18
    2 16
    3 21
    4 52
    4 22
    5 55
    5 53
    5 16
    5 14
    5 13
    5  9
    5  6
    5  2
    end
    ------------------ copy up to and including the previous line ------------------
    
    
    gsort hhid -age  // sorting so oldest would be first (because I just created the data with random ages)
    by hhid: gen n = _n
    by hhid: gen h_size = _N  // count of people in household
    bysort hhid (n): gen under15 = (age < 15)  
    bysort hhid (n): gen  over15 = (age >= 15 & age <.)
    egen count_u15 = total(under15), by(hhid)
    
    . list, noobs sepby(hhid) abbrev(14)
    
      +--------------------------------------------------------+
      | hhid   age   n   h_size   under15   over15   count_u15 |
      |--------------------------------------------------------|
      |    1    64   1        5         0        1           1 |
      |    1    54   2        5         0        1           1 |
      |    1    22   3        5         0        1           1 |
      |    1    19   4        5         0        1           1 |
      |    1    13   5        5         1        0           1 |
      |--------------------------------------------------------|
      |    2    51   1        4         0        1           0 |
      |    2    41   2        4         0        1           0 |
      |    2    18   3        4         0        1           0 |
      |    2    16   4        4         0        1           0 |
      |--------------------------------------------------------|
      |    3    21   1        1         0        1           0 |
      |--------------------------------------------------------|
      |    4    52   1        2         0        1           0 |
      |    4    22   2        2         0        1           0 |
      |--------------------------------------------------------|
      |    5    55   1        8         0        1           5 |
      |    5    53   2        8         0        1           5 |
      |    5    16   3        8         0        1           5 |
      |    5    14   4        8         1        0           5 |
      |    5    13   5        8         1        0           5 |
      |    5     9   6        8         1        0           5 |
      |    5     6   7        8         1        0           5 |
      |    5     2   8        8         1        0           5 |
      +--------------------------------------------------------+
    
    
    . tabulate hhid over15
    
               |        over15
          hhid |         0          1 |     Total
    -----------+----------------------+----------
             1 |         1          4 |         5 
             2 |         0          4 |         4 
             3 |         0          1 |         1 
             4 |         0          2 |         2 
             5 |         5          3 |         8 
    -----------+----------------------+----------
         Total |         6         14 |        20
    Note that my code counts ALL household members <15 (and not limited only to children) because I don't have a variable for children. I don't know if you have any head of households, or individuals who are married < 15, but if so you would need to change the code to account for that.
    Last edited by David Benson; 16 Jan 2019, 17:00.

    Comment


    • #3
      Hello
      thanks for your response
      I could not put the data
      I have three pillars
      1- Household code
      2. Relationships with the household head
      3. Age of household members
      In the second column, there are numbers 1 to 9 that are introduced as follows:
      1 = female
      2 = male
      3 = child
      And ...
      I need the number of children in every household 16 years and younger
      Also, the number of people over 16 years per household
      Thank you for your guidance
      Attached Files

      Comment


      • #4
        Please look at the FAQ on asking questions - provide Stata code in code delimiters, readable Stata output, and sample data using dataex. Don't post excel files - many of us won't read them due to virus concerns. If you can't provide the real data due to confidentiality concerns, either manipulate the variables (e.g., rescale) or create an example from scratch. Sometimes, using the datasets Stata uses to illustrate procedures works as well (since we can all access them easily).

        Also try to explain more clearly what you want to calculate (an example for an observation or two might help). I suspect you're looking for something like:

        bysort household: egen numlt16=count(age) if age<16

        [The age in coun(age) could be almost any variable as long as it has numeric values whenever you have a meaningful observation.]

        Comment


        • #5
          Hello
          thanks for you
          I have three columns
          FamilyCode: Reveals every household
          Relative:The ratio of household members with the household head
          Age: Age of household membersIn
          in column 2, the numbers mean this
          1: man
          2: woman
          3: child
          4, 5 and ...: grandparents, grandmothers and ...
          I need the number of children in every household 16 years and younger
          Also, the number of people over 16 years per household
          Thank you for your guidance

          copy starting from the next line -----------------------
          [CODE]
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input double FamilyCode byte(Relative Age)

          20007017925 1 52
          20007017925 2 49
          20007017925 3 21
          20003012637 1 80
          20003012637 2 70
          20003012637 3 38
          20013023737 1 71
          20013023737 2 65
          20013023725 1 48
          20013023725 2 39
          20013023725 3 21
          20013023725 3 13
          20013023725 3 11
          20001011440 1 46
          20001011440 2 39
          20001011440 3 13
          20001011440 3 5
          20001011432 1 39
          20001011432 2 34
          20001011432 3 17
          20001011432 3 4
          20001011437 1 55
          20009019134 1 81
          20009019134 2 68
          20009019125 1 54
          20009019125 2 40
          --more--


          Comment


          • #6
            Thanks for sharing your data via dataex!

            Code:
            * Could've done rename _all, lower
            rename FamilyCode family_code
            rename Relative relative
            rename Age age
            
            egen id = group(family_code)
            format family_code %13.0g
            
            * Creating the various counts
            gen is_kid = (relative==3)  // 1 if relative==3. 0 otherwise
            bysort id (relative): egen kids = total(is_kid)
            gen is_kid16 = (relative==3 & age<=16)
            bysort id (relative): egen kids16 = total(is_kid16)  // count of kids <= 16
            gen over16 = (age>16 & age<.)
            bysort id (relative): egen total_over16 = total(over16) // number of people over 16 years in household
            bysort id: gen fam_size = _N
            // Could delete the indicator variables is_kid is_kid16 over16
            
            . list id family_code relative age fam_size kids kids16 total_over16, noobs sepby(id) abbrev(14)
            
              +-----------------------------------------------------------------------------+
              | id   family_code   relative   age   fam_size   kids   kids16   total_over16 |
              |-----------------------------------------------------------------------------|
              |  1   20001011432          1    39          4      2        1              3 |
              |  1   20001011432          2    34          4      2        1              3 |
              |  1   20001011432          3    17          4      2        1              3 |
              |  1   20001011432          3     4          4      2        1              3 |
              |-----------------------------------------------------------------------------|
              |  2   20001011437          1    55          1      0        0              1 |
              |-----------------------------------------------------------------------------|
              |  3   20001011440          1    46          4      2        2              2 |
              |  3   20001011440          2    39          4      2        2              2 |
              |  3   20001011440          3    13          4      2        2              2 |
              |  3   20001011440          3     5          4      2        2              2 |
              |-----------------------------------------------------------------------------|
              |  4   20003012637          1    80          3      1        0              3 |
              |  4   20003012637          2    70          3      1        0              3 |
              |  4   20003012637          3    38          3      1        0              3 |
              |-----------------------------------------------------------------------------|
              |  5   20007017925          1    52          3      1        0              3 |
              |  5   20007017925          2    49          3      1        0              3 |
              |  5   20007017925          3    21          3      1        0              3 |
              |-----------------------------------------------------------------------------|
              |  6   20009019125          1    54          2      0        0              2 |
              |  6   20009019125          2    40          2      0        0              2 |
              |-----------------------------------------------------------------------------|
              |  7   20009019134          1    81          2      0        0              2 |
              |  7   20009019134          2    68          2      0        0              2 |
              |-----------------------------------------------------------------------------|
              |  8   20013023725          1    48          5      3        2              3 |
              |  8   20013023725          2    39          5      3        2              3 |
              |  8   20013023725          3    13          5      3        2              3 |
              |  8   20013023725          3    21          5      3        2              3 |
              |  8   20013023725          3    11          5      3        2              3 |
              |-----------------------------------------------------------------------------|
              |  9   20013023737          1    71          2      0        0              2 |
              |  9   20013023737          2    65          2      0        0              2 |
              +-----------------------------------------------------------------------------+

            Comment


            • #7

              Hello
              Thank you very much Dear Professor David Benson
              I entered the code, I see the following errors
              I do not know what to do in this situation
              ************************************************** **
              rename FamilyCode family_code
              . rename Relative relative
              . rename Age age
              . egen id = group(family_code)

              . format family_code %13.0g
              string %fmt required for string variables
              r(120);


              . gen is_kid = (relative==3) // 1 if relative==3. 0 otherwise
              unknown function ()
              r(133);


              . bysort id (relative): egen kids = total(is_kid)
              is_kid not found
              r(111);


              . gen is_kid16 = (relative==3 & age<=16)
              type mismatch
              r(109);


              . bysort id (relative): egen kids16 = total(is_kid16) // count of kids <= 16
              varlist not allowed
              r(101);

              . gen over16 = (age>16 & age<.)
              type mismatch
              r(109);

              . bysort id (relative): egen total_over16 = total(over16) // number of people over 16 years in house
              > hold
              varlist not allowed
              r(101);

              ************************************************** ************************************************** *******
              Please guide me
              Thank you

              Comment


              • #8
                Hello
                Someone does not help me?

                Comment


                • #9
                  fereshte yousefzade ,

                  This is just a guess, but based on the output above, especially the "unknown function ()", I'll bet it's a problem due to pasting the code directly from Statalist into Stata or a do file. (I've had similar issues--apparently the Statalist text editor has some non-printing (and thus invisible to us) characters that Stata doesn't like. I notice it particularly in lines with the // comment delimiter. Sometimes I will also exit Stata, restart it, and then try manually typing the comments into Stata or a do file.

                  Some of these problems then cascade down -- for example, the variable is_kid isn't created in line 2, therefore in line 3, Stata gives you the error "is_kid not found."

                  Short answer--I would try manually typing in those commands again.

                  Comment

                  Working...
                  X