Announcement

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

  • Difference of a variable with time and id

    Hi
    My data is of the following form:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(month id return)
    555  1 .00057206413
    555  2   -.07322104
    555  3   -.06859436
    555  4   -.07735524
    555  5   -.03938038
    555  6   .007824589
    555  7   -.04908807
    555  8   .003092327
    555  9   -.10695495
    555 10   -.04521626
    556  1   -.04298264
    556  2   -.05878122
    556  3  -.033808254
    556  4  -.011589252
    556  5  -.017557062
    556  6  -.004073501
    556  7   .001721355
    556  8  -.010957217
    556  9   .015607742
    556 10  -.013908166
    557  1  -.010737965
    557  2  -.018535594
    557  3    .03754317
    557  4   -.07786346
    557  5  -.074161746
    557  6   -.05729289
    557  7   -.12252308
    557  8    -.0617717
    557  9  -.020674044
    557 10   -.03216382
    558  1    .03763035
    558  2   .009898664
    558  3   .024982596
    558  4   .027306067
    558  5    .02957879
    558  6    .02146479
    558  7   .032690376
    558  8    .02783219
    558  9    .02452107
    558 10    .03302523
    559  1    .02270367
    559  2  .0017711997
    559  3   .014527231
    559  4   .017970225
    559  5   .007367407
    559  6   .031037053
    559  7   .028186474
    559  8   .023721296
    559  9   .019167695
    559 10   .018264148
    560  1    .04948542
    560  2    .03846701
    560  3    .02795666
    560  4    .02451953
    560  5   .037013665
    560  6   .034662504
    560  7    .04399181
    560  8    .04507814
    560  9    .04191253
    560 10     .0580038
    561  1   .019825436
    561  2   .032455444
    561  3    .03048469
    561  4    .01998458
    561  5   .011587504
    561  6    .02427252
    561  7    .03101354
    561  8   .015491064
    561  9    .01145711
    561 10   .017419985
    562  1   .003733994
    562  2  -.002126549
    562  3 -.0025371325
    562  4   .015725277
    562  5  -.005115598
    562  6   .006009568
    562  7   .004166465
    562  8   .011407542
    562  9   .004518013
    562 10   .002893288
    563  1    .01868514
    563  2   .014283576
    563  3    .01902623
    563  4   .012530405
    563  5   .021287354
    563  6   .008199372
    563  7    .01771125
    563  8   .019615415
    563  9    .01664819
    563 10  -.006281366
    564  1  -.004248609
    564  2  .0010157756
    564  3  -.007604746
    564  4    .01184316
    564  5 .00020535664
    564  6  -.003318424
    564  7  -.007787342
    564  8   .005721211
    564  9  -.004952917
    564 10   .006883996
    end
    format %tm month
    For every month (I guess the months have been converted to Stata numbers, when I pasted them) I want to take the difference in returns between stocks with id 10 and id 1. Would appreciate if someone could help me out in taking the difference. Thank You.

  • #2
    Assuming that in your real data the id's also really run from 1 to 10 with no gaps:
    Code:
    by month (id), sort: gen wanted = return[10] - return[1]
    (I guess the months have been converted to Stata numbers, when I pasted them)
    No, nothing has been converted to numbers. That variable has always been those numbers in your data. It is the %tm format applied to them that causes them to look like monthly dates to your eye when you -list- or -browse- them. But within Stata they are, and have always been, those numbers. This is exactly how Stata represents date and time variables. Read -help datetime- and -help datetime display formats-.
    Last edited by Clyde Schechter; 06 Mar 2020, 15:04.

    Comment


    • #3
      Thanks Clyde for the help. I probably didn't realized that it was this simple. Thank You again.

      Comment

      Working...
      X