Announcement

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

  • How to recode a trinary variable into a binary

    Hello. I'm new to Stata, and I have a quick question.
    I have a variable "size" in my data base :
    1 if small
    2 if medium
    3 if large

    I am looking to recode it into a binary variable of category 0 to 1 (1 being small and 0 being medium-large). I also have to rename the tags so that in my database, the variable displays "small" and "medium-large", rather than 1 and 0. I'd rather do that in one line (but if it is more, it's not a big issue). How can I do ?

    Thank you for your help.

  • #2
    try the following:
    Code:
    gen byte newvar=size==1
    if there are missing values in "size" you will need to add:
    Code:
    replace newvar=. if size==.
    the above assumes that size is numeric - please read the FAQ on how to ask questions and how to show your data

    Comment


    • #3
      One line is certainly possible.

      Code:
      gen byte wanted = cond(size == 1, 1, cond(size == 2 | size == 3, 0, .))
      or see

      Code:
      help recode

      Comment

      Working...
      X