Announcement

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

  • Subtract variables when other variable condition is met

    Hi everyone,
    I'm fairly new at using stata and am having trouble figuring out the code for something.
    I have a variable called AnxietyTScore. The score is measured at different points denoted by the variable Assmnt (1, 2 or 3). I need to calculate the difference in scores at Assmnt 1 and 2. The study has defined a meaningful change as a change of 5 in either direction. I then need to tabulate how many scores remained same, increased or decreased.
    This is what the data looks like:

    input float(Assmnt AnxietyTScore)
    1 61.3
    2 61.1
    3 60.7
    1 49.9
    2 49.9
    3 46.5
    1 54.6
    2 48.8
    3 50.7
    1 56.6
    2 55.6
    3 56.5
    1 58.7
    1 43.9
    2 54.7
    3 75.8
    1 67.8
    2 55.1
    3 48.3
    1 65.5
    2 56.4
    3 57.5
    1 54
    2 51.1
    3 51.3
    1 67.7
    2 62.5
    3 55.9
    1 59.4
    1 66.7
    2 60.4
    3 59.6
    1 58.5
    2 55.5
    1 37.1
    2 37.1
    3 37.1
    1 63.3
    2 43.5
    3 43.8
    1 37.1
    2 54.8
    3 46.6
    1 37.1
    2 37.1
    1 51.2
    2 51.5
    3 48.3
    1 48.8
    2 47.9
    3 57
    1 48.7
    2 52
    3 69.8
    1 54.6
    2 48.5
    3 37.1
    1 62.4
    2 63.3
    3 37.1
    1 37.1
    2 37.1
    3 45.9
    1 61.2
    2 66.2
    3 64.7
    1 59.2
    2 54.2
    3 43.5
    1 58.7
    2 48.3
    3 51
    1 43.7
    2 37.1
    3 37.1
    1 69
    2 71.9
    1 61.6
    1 43.7
    1 53.7
    2 63.4
    3 57.5
    1 60.9
    2 56.8
    3 54.4
    1 58.6
    2 53.2
    3 46.1
    1 64.4
    2 51.3
    3 43.5
    1 37.1
    3 37.1
    1 37.1
    2 37.1
    3 57.1
    1 79.4
    2 76.4
    1 61.4
    2 54
    end
    [/CODE]

    Thank you!

  • #2
    your write-up is not completely clear but I think this is what you request:

    Code:
    * calculate difference
    gen wanted= AnxietyTScore-AnxietyTScore[_n-1] if Ass==2 & Ass[_n-1]==1
    
    * categorize difference
    gen byte wanted2=0 if abs(wanted)<5
    replace wanted2=1 if wanted<=-5
    replace wanted2=2 if wanted>=5 & wanted<.
    
    *label the categories
    la def wanted2 0 "same" 1 "decrease" 2 "increase"
    la val wanted2 wanted2
    you should change the variable names to ones that you want

    Comment


    • #3
      You surely and sorely need an identifier if you don't have one already. That's the first step. Here that's problematic. I've assumed that everyone has taken Assessment 1 which may be optimistic as there seem to be cases where Ass't 2 or Ass't 3 has been missed.

      Code:
      clear
      
      input float(Assmnt AnxietyTScore)
      1 61.3
      2 61.1
      3 60.7
      1 49.9
      2 49.9
      3 46.5
      1 54.6
      2 48.8
      3 50.7
      1 56.6
      2 55.6
      3 56.5
      1 58.7
      1 43.9
      2 54.7
      3 75.8
      1 67.8
      2 55.1
      3 48.3
      1 65.5
      2 56.4
      3 57.5
      1 54
      2 51.1
      3 51.3
      1 67.7
      2 62.5
      3 55.9
      1 59.4
      1 66.7
      2 60.4
      3 59.6
      1 58.5
      2 55.5
      1 37.1
      2 37.1
      3 37.1
      1 63.3
      2 43.5
      3 43.8
      1 37.1
      2 54.8
      3 46.6
      1 37.1
      2 37.1
      1 51.2
      2 51.5
      3 48.3
      1 48.8
      2 47.9
      3 57
      1 48.7
      2 52
      3 69.8
      1 54.6
      2 48.5
      3 37.1
      1 62.4
      2 63.3
      3 37.1
      1 37.1
      2 37.1
      3 45.9
      1 61.2
      2 66.2
      3 64.7
      1 59.2
      2 54.2
      3 43.5
      1 58.7
      2 48.3
      3 51
      1 43.7
      2 37.1
      3 37.1
      1 69
      2 71.9
      1 61.6
      1 43.7
      1 53.7
      2 63.4
      3 57.5
      1 60.9
      2 56.8
      3 54.4
      1 58.6
      2 53.2
      3 46.1
      1 64.4
      2 51.3
      3 43.5
      1 37.1
      3 37.1
      1 37.1
      2 37.1
      3 57.1
      1 79.4
      2 76.4
      1 61.4
      2 54
      end
      
      gen id = sum(Assmnt == 1)
      
      bysort id (Assmnt) : gen difference = Anxiety[2] - Anxiety[1] if Assmnt[1] == 1 & Assmnt[2] == 2
      format difference %2.1f
      
      gen change = cond(abs(difference) > 5, sign(difference), 0)
      
      tabdisp id, c(difference change)
      
      egen tag = tag(id)
      
      tab change if tag
      Code:
      . tabdisp id, c(difference change)
      
      ----------------------------------
             id | difference      change
      ----------+-----------------------
              1 |       -0.2           0
              2 |        0.0           0
              3 |       -5.8          -1
              4 |       -1.0           0
              5 |                      
              6 |       10.8           1
              7 |      -12.7          -1
              8 |       -9.1          -1
              9 |       -2.9           0
             10 |       -5.2          -1
             11 |                      
             12 |       -6.3          -1
             13 |       -3.0           0
             14 |        0.0           0
             15 |      -19.8          -1
             16 |       17.7           1
             17 |        0.0           0
             18 |        0.3           0
             19 |       -0.9           0
             20 |        3.3           0
             21 |       -6.1          -1
             22 |        0.9           0
             23 |        0.0           0
             24 |        5.0           0
             25 |       -5.0           0
             26 |      -10.4          -1
             27 |       -6.6          -1
             28 |        2.9           0
             29 |                      
             30 |                      
             31 |        9.7           1
             32 |       -4.1           0
             33 |       -5.4          -1
             34 |      -13.1          -1
             35 |                      
             36 |        0.0           0
             37 |       -3.0           0
             38 |       -7.4          -1
      ----------------------------------
      
      .
      . egen tag = tag(id)
      
      .
      . tab change if tag
      
           change |      Freq.     Percent        Cum.
      ------------+-----------------------------------
               -1 |         12       36.36       36.36
                0 |         18       54.55       90.91
                1 |          3        9.09      100.00
      ------------+-----------------------------------
            Total |         33      100.00
      Last edited by Nick Cox; 26 Sep 2023, 09:47.

      Comment

      Working...
      X