Announcement

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

  • Differences between two observations within group

    Hello Statlist, I need to calculate differences between avi_study_day by non-missing BAMid within each studyid. For example, for studyid=0, I like to generate a var=10 between BAMid=2 and BAMid=1, and then var=7 between BAMid=3 and BAMid=2. Thank you for helping this out!
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int(studyid avi_study_day) BAMid
    0   2  1
    0   4  .
    0   6  .
    0  12  2
    0  19  3
    0  33  4
    0  43  5
    0  58  6
    0  63  .
    0  64  .
    0  75  7
    0  77  .
    0 104  8
    0 122  9
    0 129 10
    0 135  .
    0 152 11
    0 166 12
    0 175 13
    1   2  1
    1  12  .
    1  18  2
    1  28  3
    2   2  .
    2   3  .
    2   6  1
    2   8  .
    2  13  2
    2  15  .
    2  16  .
    2  20  3
    2  22  .
    2  24  .
    2  25  .
    2  28  4
    2  29  .
    2  33  .
    2  34  .
    2  36  5
    2  38  .
    2  39  .
    2  43  6
    2  44  .
    2  50  .
    2  55  7
    2  62  .
    2  63  8
    2  68  .
    2  74  .
    2  76  9
    2  77  .
    2  82  .
    2  89 10
    2  96  .
    2  97 11
    2 101  .
    2 103  .
    2 107 12
    2 109  .
    2 131  .
    2 139  .
    2 158  .
    4   1  1
    4   2  .
    4  16  .
    4  17  2
    4  21  .
    4  24  .
    4  33  3
    4  40  .
    4  41  .
    4  46  4
    4  47  .
    4  50  .
    4  53  .
    4  55  5
    4  57  .
    4  60  .
    4  62  .
    4  63  .
    4  64  6
    4  68  .
    4  70  .
    4  71  7
    4  72  .
    4  75  .
    4  77  .
    4  83  .
    8   6  .
    8   8  1
    8   9  .
    8  13  .
    8  20  2
    8  22  .
    8  25  .
    8  28  .
    8  30  .
    8  31  .
    8  37  .
    8  43  3
    end

  • #2
    Do you want the differences for all possible pairs of values of BAMid (there are 91 of them for id = 1) or just for consecutive values of BAMid?

    Also, do you want these results just displayed in the Results window, or do you want to create new variables containing them?

    Comment


    • #3
      Thank you Clyde! I want both for pair differences and the current BAMid-the first BAMid (if that's what you mean by consecutive). And I like new variables for them. Thanks very much!

      Comment


      • #4
        Clear on the new variables, but still confused about which BAMid differences to calculate. In #1, you cite BAMid=3 minus BAMid=2, as an example, but the first BAMid isn't part of that.

        Comment


        • #5
          Sorry for my confusion. I can use BAMid=2 minue BAMid=1 to get the differences of avi_study_day. I hope to get the duration of days in the interval between BAMid within each studyid. Thank you for helping it out!!

          Comment


          • #6
            I still don't understand which pairs of BAMid you want to contrast. I'll just assume you want to contrast consecutive values of BAMid, because that's the simplest. If this isn't what you want, post back showing what you want the final results to look like in the data set.

            Code:
            gen long obs_no = _n    // PRESERVE ORIGINAL SORT ORDER
            by studyid BAMid, sort: egen mean_avi_study_day = mean(avi_study_day) if !missing(BAMid)
            by studyid (BAMid): gen diff_mean_avi = mean_avi_study_day - mean_avi_study_day[_n-1]
            
            sort obs_no

            Comment


            • #7
              Thank you Clyde! It may be my fault haven't explained it clearly very much. Think the ideal dataset would be creating a new var "duration". For example, for studyid=0, the duration for avi_study_day is 0, but when BAMid becomes 2, the duration=12-2 using avi_study_day. The duration is the differences between avi_study_day when BAMid changes within each studyid. Hope this time works. Thank you very much for helping this out.
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input studyid avi_study_day BAMid duration
              0 2 1 0
              0 4 . .
              0 6 . .
              0 12 2 10
              0 19 3 7
              0 33 4 14
              0 43 5 10
              0 58 6 15
              0 63 . .
              0 64 . .
              0 75 7 17
              0 77 . .
              0 104 8 29
              0 122 9 .
              0 129 10 .
              0 135 . .
              end
              [/CODE]

              Comment


              • #8
                Well, that's what my code does. I just called it diff_mean_avi instead of duration. If you run my code you will see that it produces the same results as your variable duration, except for two observations. You have no value shown for duration when BAMid == 9 and BAMid == 10, whereas my code calculates the differences as 18 and 7, respectively. I don't understand why you don't calculate a difference for those two BAMid's. There is nothing obvious that distinguishes them.

                Comment


                • #9
                  Thank you Clyde! The code works! Sorry for my late reply!!

                  Comment

                  Working...
                  X