Announcement

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

  • Create a Variable in stata that is a value minus it's last month value

    Hi all,

    I put some data below, but I have a question about making a varialbe that is it's value minus the value of the last month. I have a variable activemonth_final that is in the format 2020m1, 2020m2, 2020m3, etc. The variable ccda_all_act_ind has a different value for each month. Is there a way I can make a variable that shows ccda_all_act_ind minus it's value from last month? For example, in the 2020m1 case it would show 2020m1-2019m12. Thanks!
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(activemonth_final ccda_all_act_ind)
    611    .
    647 2206
    587    .
    671 2486
    659 2241
    623    .
    719 3084
    695 2970
    599    .
    707 3079
    683 2793
    635    .
    684 2875
    708 3209
    672 2517
    612    .
    588    .
    720 3213
    660 2230
    624    .
    636 2077
    696 3160
    600    .
    648 2178
    697 3160
    721 3200
    649 2196
    685 2816
    613    .
    589    .
    709 3147
    661 2213
    625    .
    637 2047
    673 2658
    601    .
    710 3178
    638 2182
    650 2297
    686 2980
    698 3226
    602    .
    674 2746
    626    .
    614    .
    494    .
    662 2250
    590    .
    722 3195
    615    .
    663 2231
    627    .
    603    .
    699 3198
    639 2299
    579    .
    723 2941
    675 2637
    711 3182
    591    .
    651 2256
    687 2917
    712 3172
    652 2230
    676 2743
    700 3215
    616    .
    724 2973
    628    .
    688 2990
    604    .
    640 2295
    592    .
    664 2227
    677 2699
    701 3126
    689 3067
    629    .
    593    .
    725 3051
    617    .
    641 2231
    653 2194
    713 3051
    665 2299
    605    .
    642 2276
    678 2672
    594    .
    654 2231
    702 3076
    630    .
    606    .
    666 2349
    726 2983
    690 2981
    714 3180
    582    .
    618    .
    703 3147
    end
    format %tm activemonth_final
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 152 observations
    Use the count() option to list more

    .

  • #2

    Code:
    tsset activemonth 
    gen wanted = D.ccda

    Comment


    • #3
      Code:
      tsset activemonth_final
      gen wanted = ccda_all_act_ind - l.ccda_all_act_ind

      Comment


      • #4
        Thank you so much!

        Comment


        • #5
          Is there a way to do this if I wanted the value from last *year*? For instance, if I wanted ccda of august 2019-ccda of august 2018?

          Comment


          • #6
            Originally posted by Lindsey Buck View Post
            Is there a way to do this if I wanted the value from last *year*? For instance, if I wanted ccda of august 2019-ccda of august 2018?
            This should work:

            Code:
            gen wanted = ccda_all_act_ind - l12.ccda_all_act_ind

            Comment


            • #7
              I am running into issues using tsset because activemonth_final is repeated in my data (but ccda is the same across the repeated observations.) any idea?

              Comment


              • #8
                How does that arise? Do you have panel data? If duplicate values make sense, then differences across time aren't uniquely defined. For example, if there were two values 12 months earlier, what would be the right answer to #5?

                Comment

                Working...
                X