Announcement

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

  • Ratio of male to female in a dataset

    Hello. It's probably an easy question but I searched a lot and found nothing.


    I have a data set and there is a variable indicting whether the observation is male or female( 1 for male, 2 for female). I want to make a table in which there is male to female ratio ( not male to total population ratio which tab command gives)

    Thanks a lot

  • #2
    Code:
    assert !missing(male)
    recode male (2=0)
    count if male
    local M= r(N)
    count if !male
    local F= r(N)
    gen wanted= `M'/`F'
    lab var wanted "Male: Female ratio"
    tabstat var1 var2 wanted
    Last edited by Andrew Musau; 09 Mar 2021, 06:39.

    Comment


    • #3
      I suspect that you have more structure to your data than you are telling us about. Let's assume a grouping variable and keep to 1 as male and 2 as female although as Andrew Musau implies you'd be better off with a (0, 1) indicator variable for most Stata purposes.


      Code:
      bysort group : egen males = total(male == 1) 
      
      by group : egen females = total(male == 2)
      and then your ratio follows. You don't need more tricks for more grouping variables, as a different prefix like

      Code:
      bysort group region year :
      would just calculate accordingly.

      Comment


      • #4
        Thanks for your great responses!

        Comment

        Working...
        X