Announcement

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

  • Counting number of people by gender

    Hi all,

    I am currently using Stata Version 14. In the sample data attached below, I have information on the gender, rank, area of residence and also the year of residence for individuals. Data is also sorted by the rank i.e. rank 1 being the top rank, followed by rank 2 and so on.

    In the data attached, if variable male=0, then the individual is a female and if male=1, then the person is a male. What I would like to do is, identify the number of males and females above their rank order (i.e. rank 1 is above/better than rank 2 and so on), only for females. For ex, in area B, person of rank 2 is a female. Number of males above this individual's rank is 1 (as the person of rank 1 is a male) and the number of females above their rank is zero. Likewise, for area B again, person of rank 5 is a female. Number of males above their rank order is 3 and number of females above their rank is 1.

    Any suggestions on how to execute this using code would be helpful.

    Thanks.


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str29 area float(year rank male males_above females_above female)
    "A" 1978 1 1 . . 0
    "A" 1978 2 1 . . 0
    "A" 1978 3 0 2 0 1
    "A" 1978 4 1 . . 0
    "A" 1978 5 0 3 1 1
    "B" 1983 1 1 . . 0
    "B" 1983 2 0 1 0 1
    "B" 1983 3 1 . . 0
    "B" 1983 4 1 . . 0
    "B" 1983 5 0 3 1 1
    end

  • #2
    Vignesh:
    I'm not clear with what you're after.
    Anyway, I do hope that what follows can support your analysis:
    Code:
    . bysort female area : count if rank>1
    
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> female = Female, area = A
      2
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> female = Female, area = B
      2
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> female = Male, area = A
      2
    ---------------------------------------------------------------------------------------------------------------------------------------
    -> female = Male, area = B
      2
    
    .
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Hi Carlos,

      Thanks for your reply. The code you have provided just sorts data in order by female and area.

      What I would like to do instead is, create variables 'males_above' and 'females_above' as I have outlined in my post, which counts the number of males ranked above a female of particular order and also number of females ranked above a female of particular order, respectively.

      As an example, if you take area A: individual of rank 3 is a female. There are two people above her rank and both are males. Therefore, the variable 'males_above' should take on value 2 and 'females_above' should take on value zero.

      Likewise, for the same area A: individual of rank 5 is a female. There are four people above her rank and three are males, whereas one of them is a female. Therefore, the variable 'males_above' should take on value 3 and 'females_above' should take on value 1, for this particular individual.

      Regards,
      Vignesh.

      Comment


      • #4
        Vignes:
        I would explore whether one of the -egen- functions can do the trick.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          Thanks, Carlo.
          It has been sorted out. I calculated it as a cumulative frequency.

          Regards
          Vignesh.

          Comment

          Working...
          X