Announcement

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

  • Generating a month-year variable

    Hello All,

    I am working with a dataset that contains the returns of firms at at different dates over the years. Firms are denoted by "PERMNO", date of the observation denoted by "date", and return denoted by "RET". What I would like to be able to do is to create a sort of month-year variable for each observation. So for example, if there is an observation that occurs on 02jan19997 according to the "date" variable, this new month-year variable should list this observation as occurring on m1-1997. Or as another example, 01feb1997 becomes m2-1997. Those anyone know how this can be done?

    Here is the code I am working with:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double PERMNO long date double RET
    10078 13516    .04136253148317337
    10078 13517   .037383176386356354
    10078 13520   .003941441420465708
    10078 13521  -.021873246878385544
    10078 13522   -.03440367057919502
    10078 13523   .030878860503435135
    10078 13524   .004608294926583767
    10078 13527    .01834862306714058
    10078 13528    .04054053872823715
    10078 13529  -.021645022556185722
    10078 13530    .09955751895904541
    10078 13531  .0040241447277367115
    10078 13534    .03807615116238594
    10078 13535     .0308880303055048
    10078 13536  -.018726591020822525
    10078 13537   .007633587811142206
    10078 13538   -.03787878900766373
    10078 13541  -.015748031437397003
    10078 13542    .03200000151991844
    10078 13543  .0019379844889044762
    10078 13544  -.021276595070958138
    10078 13545   .003952569328248501
    10078 13548   .035433072596788406
    10078 13549  -.011406844481825829
    10078 13550   .003846153849735856
    10078 13551  -.007662835065275431
    10078 13552   .050193049013614655
    10078 13555 -.0036764706019312143
    10078 13556  -.014760147780179977
    10078 13557    .03745318204164505
    10078 13558   .010830325074493885
    10078 13559  -.010714286006987095
    10078 13563   -.02527075819671154
    10078 13564    .02777777798473835
    10078 13565  -.030630631372332573
    10078 13566   -.01858736015856266
    10078 13569 -.0037878789007663727
    10078 13570 -.0038022813387215137
    10078 13571   .003816793905571103
    10078 13572   -.03041825070977211
    10078 13573    -.0313725508749485
    10078 13576  -.024291498586535454
    10078 13577   .016597511246800423
    10078 13578  -.040816325694322586
    10078 13579     .0382978729903698
    10078 13580  -.026639344170689583
    10078 13583    .05263157933950424
    10078 13584    .01600000075995922
    10078 13585   -.06299212574958801
    10078 13586  -.004201680887490511
    10078 13587   .016877636313438416
    10078 13590   -.04149377718567848
    10078 13591  -.025974025949835777
    10078 13592    -.0533333346247673
    10078 13593    .07981220632791519
    10078 13594   -.03478260710835457
    10078 13597   .036036036908626556
    10078 13598  -.004347825888544321
    10078 13599    .06550218164920807
    10078 13600    -.0450819656252861
    10078 13604  -.008583690971136093
    10078 13605   -.05627705529332161
    10078 13606                     0
    10078 13607    .04128440469503403
    10078 13608   .030837005004286766
    10078 13611   .017094017937779427
    10078 13612   .006302521098405123
    10078 13613  -.014613778330385685
    10078 13614  -.016949152573943138
    10078 13615   -.03448275849223137
    10078 13618     .0669642835855484
    10078 13619  -.016736401244997978
    10078 13620    -.0659574493765831
    10078 13621   .015945330262184143
    10078 13622  -.026905829086899757
    10078 13625  -.009216589853167534
    10078 13626   .009302325546741486
    10078 13627                     0
    10078 13628   .036866359412670136
    10078 13629  -.035555556416511536
    10078 13632   .032258063554763794
    10078 13633  -.004464285913854837
    10078 13634    .03363228589296341
    10078 13635  -.006507592275738716
    10078 13636   .021834060549736023
    10078 13639    .02350427396595478
    10078 13640    .00626304792240262
    10078 13641    .02489626593887806
    10078 13642   .016194332391023636
    10078 13643   -.01593625545501709
    10078 13646  -.010121457278728485
    10078 13647   .008179958909749985
    10078 13648    .03448275849223137
    10078 13649    .03529411926865578
    10078 13650   -.02651515230536461
    10078 13653  -.019455252215266228
    10078 13654     .0476190485060215
    10078 13655                     0
    10078 13656   .007575757801532745
    10078 13657    .01879699155688286
    end
    format %td date

  • #2
    Code:
    gen wanted = ym(year(date),month(date))
    format %tm wanted

    Comment


    • #3
      See #2 of your previous thread https://www.statalist.org/forums/for...onthly-returns where the answer was given: use mofd().

      #2 of this thread is quite right, but I'd still recommend one function call -- to mofd() -- rather than three.

      Comment


      • #4
        Thank you both for your answers. I followed what you advised me to do Nick, with regards to daily returns over the past three months.

        Comment

        Working...
        X