Announcement

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

  • Looping over gender and total sample

    Hi,

    I have a typical sex variable where 1 is male and 2 is female. I am trying to build a value 3 which would be everyone, so that I might have a loop that goes from male to female, to everyone.

    I am really struggling for some reason to build number 3. I thought this would be simple but it is not proving to be. Does anyone have any suggestions?

    This is what I have now but its not working.

    Code:
    gen allsex = .
    egen allsex1 = max(sex == 1 | sex == 2)
    replace allsex = 3 if allsex1 == 1
    replace allsex = 1 if sex == 1
    replace allsex = 2 if sex == 2

  • #2
    You cannot have what you want, as it would require a person to have more than one value on the sex variable. (I think you misunderstand how variables work in a program like Stata.) Fortunately, what you want is not something necessary or useful. I'd guess that you want to run some command (say -summarize-) separately for each gender and for the whole sample. There are many ways to do this. Here are several, to demonstrate techniques:
    Code:
    // Preferred approach.
    summarize YourVariable   // regardless of the value of sex
    // Each gender separately.  See -help by-, a powerful feature of Stata.
    bysort sex: summarize YourVariable
    //
    // Another way
    summarize YourVariable
    summarize YourVariable if sex == 1
    summarize YourVariable if sex == 2
    A loop could be devised to do what you want, and might in some circumstance be useful if your variable, unlike sex, had many different values rather than just a few. Here's that technique, which is not desirable here:
    Code:
    // Another way
    summarize YourVariable
    levelsof sex, local(sexvalues)
    foreach v of local sexvalues {
        summarize YourVaraible if sex == `v'
    }

    Comment


    • #3
      Hi, the request was specific to me wanting to run the loop for male, female, total sample, which is specific to the needs of my model, and trying to save a bunch of copying and pasting. Not so much the need for it to be all in one variable (though that would have been handy for a loop). I do realise in what I suggested, value 3 would have been a function of 1 and 2. I just wasn't sure if there was a way around this.

      Thanks.

      Comment


      • #4
        Originally posted by Christa Hansen View Post
        Hi, the request was specific to me wanting to run the loop for male, female, total sample, which is specific to the needs of my model, and trying to save a bunch of copying and pasting. Not so much the need for it to be all in one variable (though that would have been handy for a loop). I do realise in what I suggested, value 3 would have been a function of 1 and 2. I just wasn't sure if there was a way around this.

        Thanks.
        A binary variable cannnot have three categories. You could just run three seperate sets of analyses for male, female and pooled sample.

        Comment

        Working...
        X