Announcement

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

  • nesting cond()

    hi All,

    I was reading the stata journal version: The Stata Journal (2005) 5, Number 3, pp. 413–420 in order to become familiar with the cond() command, especially to categorize some variables.

    In section 4, page 415 , the authors talk about do it yourself categorization using nested cond() command... im just wondering if anyone can share the syntax for this with me, i ve tried everything and couldnt get it to work.

    i want to create a variable that is 0 if a=0 & b=0 ; 1 if a=1 & b=0 ; 2 if a==0 & b==1 ; 3 if a==1 & b==1 .. a and b are binary variables i have coded already.

    i could use generate and replace commands, but i would like to use the cond() if possible

    thanks!

  • #2

    Code:
    gen wanted = cond(a==0 & b==0, 0, cond(a==1 & b==0, 1, cond(a==0 & b==1, 2, 3)))
    I don't favour cond() whatever the context. I would wave a flag for


    Code:
    gen wanted =  0 if a==0 & b==0
    replace wanted = 1 if a==1 & b==0
    replace wanted = 2  if a==0 & b==1
    replace wanted = 3 if a==1 & b==1
    or

    Code:
    gen wanted = a + 2 * b

    as likely being clearer to more people.

    Comment

    Working...
    X