Announcement

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

  • Time variable in inrange?

    Dear All,

    I have a portion of my data set as shown below:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float ym double rf
    -408              .002951
    -407              .002768
    -406              .002778
    -405              .003072
    -404              .000342
    -403 .0034590000000000003
    -402  .002199999988079071
    -401 .0024999999441206455
    -400  .002300000051036477
    -399 .0031999999191612005
    -398  .003100000089034438
    -397   .00279999990016222
    -396 .0024999999441206455
    -395 .0026000000070780516
    -394  .003000000026077032
    -393 .0024999999441206455
    -392  .003000000026077032
    -391 .0026000000070780516
    -390  .003000000026077032
    -389   .00279999990016222
    -388  .002099999925121665
    -387 .0024999999441206455
    -386  .002099999925121665
    -385  .002199999988079071
    -384 .0024999999441206455
    -383 .0033000002149492502
    -382  .002899999963119626
    -381  .002199999988079071
    -380 .0031999999191612005
    -379  .003100000089034438
    -378 .0031999999191612005
    -377 .0031999999191612005
    -376 .0027000000700354576
    -375  .004100000020116568
    -374  .003800000064074993
    -373 .0005999999702908099
    -372 .0034000000450760126
    -371 .0036000001709908247
    -370 .0034000000450760126
    -369 .0036000001709908247
    -368  .004399999976158142
    -367  .005200000014156103
    -366 .0033000002149492502
    -365  .004000000189989805
    -364  .003499999875202775
    -363  .004600000102072954
    -362  .003700000001117587
    -361  .003700000001117587
    -360   .00139999995008111
    -359  .003000000026077032
    end
    format %tm ym
    I am trying to generate a categorical variable (cat) with values 1, 2, and 3 representing the following time ranges:
    1. 1926/01 - 1988/12
    2. 1926/01 - 1957/06
    3. 1957/07 - 1988/12
    I would appreciate any suggestions on how to achieve this.

    Thank you in advance!
    Ho-Chuan (River) Huang
    Stata 17.0, MP(4)

  • #2
    Groups 2 and 3 are mutually exclusive but group 1 includes both. So there is no scope for a variable with values 1, 2, 3.

    Comment


    • #3
      Many thanks, Nick. My initial idea was to generate a variable cat. If the time falls between 1926/01 and 1988/12, then cat is assigned a value of 1; if it's between 1926/01 and 1957/06, then cat is assigned a value of 2; and if it's between 1957/07 and 1988/12, then cat is assigned a value of 3. I wonder if this is feasible?
      Ho-Chuan (River) Huang
      Stata 17.0, MP(4)

      Comment


      • #4
        I still don't follow unless you are able to give examples where you know that 1 is true but you can't be clear which of 2 or 3 applies. Your data example includes what looks like a Stata monthly date, so there is no ambiguity or uncertainty about the date.

        Comment


        • #5
          Dear Nick, Now I think your concern is correct. The way I do it is:
          Code:
          gen date = ym + 408 + 1  // 
          
          * 1926/01~1988/12
          gen sample1 = (date >= 1 & date <= 756)  // 1926/01 到 1988/12
          
          * 1926/01~1957/06
          gen sample2 = (date >= 1 & date <= 378)  // 1926/01 到 1957/06
          
          * 1957/07~1988/12
          gen sample3 = (date > 378 & date <= 756)  // 1957/07 到 1988/12
          
          * 1926/01~2023/12
          gen sample4 = (date >= 1 & date <= 1176)  // 1926/01 到 2023/12
          
          * 1989/01~2023/12
          gen sample5 = (date > 756 & date <= 1176)  // 1989/01 到 2023/12
          
          label var sample1 "1926/01-1988/12"
          label var sample2 "1926/01-1957/06"
          label var sample3 "1957/07-1988/12"
          label var sample4 "1926/01-2023/12"
          label var sample5 "1989/01-2023/12"
          
          foreach v in sample1 sample2 sample3 sample4 sample5 {
              preserve
              keep if `v' == 1
              display "`v'"
              // value-weighted
              sum vwerm 
              display (r(mean)/r(sd))^2
              //equal-weighted
              sum ewerm 
              display (r(mean)/r(sd))^2
              restore
          }

          Ho-Chuan (River) Huang
          Stata 17.0, MP(4)

          Comment

          Working...
          X