Announcement

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

  • Stata error code: Factor Variables may not contain negative values

    Hello,

    I am very new to Stata - I have tried reading around to solve my error but I am struggling! I am using StataIC 16.

    I am trying to run a regression for log hourly wages on various variables, many of which are factor variables. I have downloaded data from the labour force survey.

    Regression:

    reg LGHOURPAY AGE b(5).DEGCLS7 b(1).EMPLEN b(1).ETHUKEUL b(0).FDPCH16 b(9).FORTYP15 b(2).FTPTWK b(8).GORWKR b(15).IN0792SM b(1).MARSTA b(1).MPNR02 b(2).PUBLICR b(9).SC10MMJ b(1).SEX b(6).SNGDEGB

    This is my regression and I believe the b(). allows me to choose what base/ reference level for the factor variables. However, I am getting the "Factor Variables may not contain negative values" error code. I have looked and the "don't know" levels for each variable have group number of -9.

    How would I overcome this problem?

    Thanks!

  • #2
    So, you should replace the values of -9 with a positive value not in use. Alternatively, decide that don't know is in effect missing.

    Comment


    • #3
      Hi Nick,

      Thank you for this. How would I go about replacing the values of -9 to a positive value? And also, how would I go about changing that level of a categorical variable to be a missing effect? Sorry for the lots of questions!

      Comment


      • #4
        Olivia Roberts

        1) I like to work with lowercase variable names so here is the code for that:

        Code:
        rename *, lower
        Here is the code for changing a single variable's missing indicator from -9 to Stata's missing value indicator which is a period. If you want it to be some other value just replace "." with a value (e.g., "99")

        Code:
        replace EMPLEN =. if EMPLEN==-9
        Here is the code for changing every variable's value of -9 to missing. Be careful, because -9 might actually mean -9 (and not missing) in one or more variables. If that is the case, you can still use the foreach loop, but you just need to specify your variables in a varlist or local macro.

        Code:
        findname, any(inlist(@, -9)) local(missing)
        foreach v of local missing {
        replace `v' = . if `v' == -9
        }

        Comment


        • #5
          That is great Tom thank you, I will have a go thank you!

          Comment

          Working...
          X