0

I have a gender column that says either "Female", "Male", or . for missing entry. I want to create a new column that gives dummy variable 1 if gender is female and 0 is gender is male (and continuing to give . for missing value). I tried to do gen Fem = gender = "Female" but this does not work and I get r(109) error.

I am assuming this is because the gender column is a byte format column.

Also, how do I interact dummy variable column with variables in a regression model on Stata?

Nick Cox
37.4k6 gold badges37 silver badges51 bronze badges
asked Mar 3, 2023 at 19:37
3
  • sounds like your gender variable is already a dummy variable, but simply has value labels. (to see this compare tab gender with tab gender, nolabel). You do not need to create a new variable. Commented Mar 3, 2023 at 19:48
  • True! Male is 1 and Female is 2. However, I want to use the dummy values to interact female with some other variables in the regression I am running. How can I do that with the current column? Commented Mar 3, 2023 at 19:52
  • In Stata we talk of variables, not columns. Also byte is a variable or storage type and not a (display) format. Commented Mar 4, 2023 at 10:10

1 Answer 1

2

See help fvvarlist, and check out the information on the binary operator #

For a specific example, see below where I create a 100 row dataset with variables y, x1, x2, and gender, the last of which should be similar to yours (i.e. byte with Male and Female labels)

clear
set obs 100
set seed 123
gen byte gender = runiform()<0.3
recode gender 0=1 1=2
label define gender 1 "Male" 2 "Female"
label values gender gender
gen x1 = runiform()<0.7
gen x2 = runiform()<0.2
gen y = runiform()<0.5

I can include a interaction term using the # operator

regress y x1 x2 x1#gender
answered Mar 3, 2023 at 20:06
Sign up to request clarification or add additional context in comments.

1 Comment

This may be a matter of taste, but I find the output of regress y x2 x1##i.gender more legible and the code easier to scan.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.