Announcement

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

  • New variables with conditions

    Hello ! I have a database where I need to create a new binary variable based on the values of 2 already existing variables. The 2 already existing variables that interests me are A (what is your profession) and B (what is the sector you're working in) because they help me identifying the professions of people interviewed. For the new variable I want to create, I want to select only people working on the transport sector starting from responses given in A and B. The new variable is going to be called Transport with 1 if they work on this sector and 0 if they don't.

    In A : I have a subset of values that is certain that I want to assign 1 in my new variable
    while I have another subset that I want to condition to the responses given in B.

    Could the egen & cond() commands help me ? Thanks !



  • #2
    Data example please. Please follow requests at https://www.statalist.org/forums/help#stata

    Detail: cond() is a function, not a command.

    Comment


    • #3
      I would do this in steps. I know I will make mistakes if I try to manage too many if conditions at the same time.

      Lets say you want the new variable wanted to 1 if ( a is 4, 7, or 12) or ( ( a is 3 or 5) and b = 8 ) and 0 otherwise. As an additional requirement we want wanted to be missing when a is missing or (b is missing and (a is not 4, 7, or 12))

      Code:
      gen wanted = .
      replace wanted = 1 if inlist(a,4,7,12)
      replace wanted = 1 if inlist(a,3,5) & b==8
      replace wanted = 0 if wanted == . & !missing(a,b)
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment

      Working...
      X