Announcement

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

  • Dummy Variable with Mutliple conditions

    I am trying to generate a new variable to take on the values of 1 and 0. Where 1 represents alcohol abuse and 0 no alcohol abuse.

    My variables are:

    Alc is a variable which has information for alcohol consumption

    gender takes the value of 0 and 1 for male and female respectively



    for female, alcohol abuse the condition is equal or greater than 2.

    for male, alcohol abuse the condition is equal or greater than 3.


    Will I be right to specific the command as below:

    Code:
    gen ABI = 0
    Code:
    replace ABI =1 if alc >=3 & gender ==1
    Code:
    replace ABI =1 if alc >=4 & gender ==0
    Clyde Schechter




  • #2
    Ayorinde,

    It looks to me like your code will get you what you want if (a) female is gender==1 and (b) you don't have any missing values for alcohol consumption. Also, I always recommend creating / renaming any gender variable to something like male or female (whichever is coded as a 1).

    I made some toy data to test things:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id alcohol_consump female)
     1 3 1
     2 6 1
     3 0 1
     4 4 1
     5 5 1
     6 0 1
     7 1 1
     8 6 1
     9 5 1
    10 3 1
    11 0 0
    12 1 0
    13 6 0
    14 1 0
    15 1 0
    16 6 0
    17 0 0
    18 6 0
    19 3 0
    20 1 0
    end
    ------------------ copy up to and including the previous line ------------------


    Code:
    tabstat alcohol_consump, by(female) stats(n mean median min max)
    
      female |         N      mean       p50       min       max
    ---------+--------------------------------------------------
           0 |        10       2.5         1         0         6
           1 |        10       3.3       3.5         0         6
    ---------+--------------------------------------------------
       Total |        20       2.9         3         0         6
    ------------------------------------------------------------
    
    gen ABI = 0   // creating alcohol abuse indicator
    replace ABI = 1 if alcohol_consump >=3 & female==1  // this works if people can't list fractions of drinks (like 2.5)
    replace ABI = 1 if alcohol_consump >=4 & female==0
    sort female alc
    list, sepby(female) noobs
    
      +------------------------------+
      | id   alcoho~p   female   ABI |
      |------------------------------|
      | 17          0        0     0 |
      | 11          0        0     0 |
      | 20          1        0     0 |
      | 14          1        0     0 |
      | 15          1        0     0 |
      | 12          1        0     0 |
      | 19          3        0     0 |
      | 16          6        0     1 |
      | 13          6        0     1 |
      | 18          6        0     1 |
      |------------------------------|
      |  6          0        1     0 |
      |  3          0        1     0 |
      |  7          1        1     0 |
      | 10          3        1     1 |
      |  1          3        1     1 |
      |  4          4        1     1 |
      |  5          5        1     1 |
      |  9          5        1     1 |
      |  2          6        1     1 |
      |  8          6        1     1 |
      +------------------------------+

    Comment


    • #3
      I always recommend creating / renaming any gender variable to something like male or female (whichever is coded as a 1).
      Bang on. The auto data does this, naturally: foreign indicates which cars are foreign (and which not) and that allows readable code such as if foreign and if !foreign.

      I have heard various tales of this form.

      Person listening to presentation: Which way is your gender dummy defined?

      Researcher: I am not sure. I will check it out.
      But go all the way: call your (0, 1) variables indicators, not dummies. More at https://journals.sagepub.com/doi/abs...36867X19830921
      Last edited by Nick Cox; 24 Apr 2019, 15:51.

      Comment

      Working...
      X