Announcement

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

  • Generate new variable

    Hey,

    in the panel study I am working with, there are two times the same variable (t34005a & t34005b). The first is only asked to people who no longer live at home and the second is only asked to those who still live at home.
    I would like to create a new variable from this so that I have all the cases.

    My idea was the following:
    // restrict variable 2 to the cases who live either with the father or with the mother.


    keep t34005a if t743024==1 | t743025==1

    then the same for the variable of persons who no longer live at home:

    keep t34005b if t743024 <>1 | t743025<>1

    and then merge, maybe with append?

    I'm a complete newbie and this is just a rough sketch.

    Thanks a lot for the answers!

  • #2
    You can keep a variable or you can keep observations but you can't mix syntaxes. Stata doesn't support <> as an operator; perhaps you mean != or ~=.

    But keep sounds wrong any way. keep can be very destructive.

    Also merge and append are for entire datasets, not variables.

    If I understand you correctly this is closer to what you need in which you take some values from one variable and the other values from another variable

    Code:
    gen wanted = cond(t743024==1 | t743025==1, t34005a, t34005b)
    That is equivalent to

    Code:
    gen wanted = t34005a if t743024 == 1 | t743025 == 1
    replace wanted = t34005b if missing(wanted)
    Naturally you should choose a variable name closer to what this represents.

    Code:
    gen wanted2 = max(t34005a, t34005b)
    might even work.

    If that doesn't help, please visit https://www.statalist.org/forums/help#stata and show us the results of


    Code:
    dataex t743024 t743025 t34005a t34005b
    Last edited by Nick Cox; 14 Apr 2021, 04:17.

    Comment


    • #3
      Thank you very much! It worked

      Have a nice day!

      Comment

      Working...
      X