Announcement

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

  • Differenzvariable über mehrere Zeilen

    Hallo zusammen,

    ich versuche gerade eine Differenzvariable aus zwei Variablen zu erstellen.

    Die Differenz soll aus zwei Variablen je einer ID gebildet werden und das für alle IDs -
    also quasi ID1 Wert Variable Z1 - ID1 Wert Variable aus Zeile 2,
    ID2 Wert Variable Z1 - ID2 Wert Variable aus Zeile 2 ...
    id wave var1 var2
    1001 2 1,2 .
    1001 4 . 1,4
    1002 2 2,0 .
    1002 4 . 2,8
    Da es zwei Wellen (Welle2 und Welle4) gibt, hat die Variable immer auf einer Welle ein Missing, da sie zu zwei verschiedenen Zeitpunkten abgefragt wurde.
    Diese Befehle:
    gen diffwarmth_pacs = warmth_pacsw2 - warmth_pacsw4

    bysort id: gen diffwarmth_pacs = warmth_pacsw4 - warmth_pacsw2

    Nun meine Frage, welcher Befehl kann eine Differenzvariable erstellen, in der die Werte var1 von den Werten var2
    abgezogen werden?

    Vielen Dank schonmal!


  • #2
    Dear Hannah,

    just as a small note: in general, you are more likely to get an answer in this forum if you ask in English.

    Turning to your question, one solution in this particular case is:

    Code:
    xtset id wave
    gen diff = var2 - l2.var1
    It calculates the difference for var2(t=0)-var1(t=-2). You could also generate a new variable which maps the value for var1 to t=0 by "gen var1_wave4 = l2.var1" (only possible if previously "xtset") and then calculate the "normal" difference via "generate".

    Best,
    Marvin

    Comment


    • #3
      Dear Marvin,

      thank you so much for your answer and the code, it worked!

      May i ask you a further question?
      I would like to know how to subtract values within one line from another.
      Moreover, this command should be done for the whole line (approx. 500 obeservations) of var 1. Do you have any idea for a code that could perform the subtraction of two values of one id for the whole line?
      id var1
      1001 1
      1001 0,8
      1002 2
      1002 1,2
      Thanks and best wishes,
      Hannah

      Comment


      • #4

        Code:
        bysort id : gen wanted = var1[2] - var1[1]
        is one answer to #3, but much depends on whether there is some other variable which defines a unique order within id.

        Comment


        • #5
          Thank you Nick, the code worked perfect.

          Comment

          Working...
          X