Announcement

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

  • Generate a new variable

    Dear all,

    I hope you can help me.

    I would like to generate a new variables based on values of another variable. The variable I would like to use has 18 values and I would like to trichotomize it into 3 categories with the values 0, 1, 3.

    I tried to use:

    gen newvardemo if country_code= 1=1 and if country_code= 2=0

    but it appears this is not valid syntax.

    Can you help me?

  • #2
    That is indeed illegal syntax. Among several other details, testing for equality always requires ==.

    Code:
    gen newvardemo = cond(country_code == 1, 1, cond(country_code == 2, 0, 3))
    maps 1 to 1, 2 to 0 and everything else to 3. You might find it easier to do it in steps:

    Code:
    gen newvardemo = 3
    replace newvardemo = 1 if country_code == 0
    replace newvardemo = 0 if country_code == 2
    I am guessing here because I can't see that you give your rule for new values of 3.

    Comment

    Working...
    X