I want to find rolling (24-60 months, as available) mean of the variable rateofreturn when it falls below the variable pct5. To be precise, the new variable, say rmean2, will have its first value in row 24; the value will be the mean of those observations(row1-24) of rateofreturn which fall below the value of pct5 in row 24. Similarly, in row 25 the variable rmean2 will contain the mean of rateofreturn (row1-25) which fall below the value of pct5 in row 25. The number of observations will continue to increase until it reaches 60.
Based on answers of my previous post, which was similar in nature, I, as a neophyte, tried the following the code.
Unofortuantely, the above code didn't work.
Thank you for your attention.
Based on answers of my previous post, which was similar in nature, I, as a neophyte, tried the following the code.
Code:
program below_pct5 sum rateofreturn if rateofreturn < rpct5 gen rmean2 = r(mean) end rangerun below_pct5, by(id) interval(mdate -59 0) use(rateofreturn pct5) sprefix(rr_)
Thank you for your attention.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(id mdate) double(rateofreturn n1) float pct5 1 420 .48 1 . 1 421 .89 2 . 1 422 1.18 3 . 1 423 1 4 . 1 424 1.5 5 . 1 425 1.6 6 . 1 426 1.3 7 . 1 427 1.01 8 . 1 428 1.44 9 . 1 429 1.55 10 . 1 430 1.2 11 . 1 431 1.51 12 . 1 432 .97 13 . 1 433 1.21 14 . 1 434 .42 15 . 1 435 .82 16 . 1 436 1.14 17 . 1 437 1.09 18 . 1 438 .18 19 . 1 439 .62 20 . 1 440 1.24 21 . 1 441 1.18 22 . 1 442 .85 23 . 1 443 1 24 .42 1 444 .7 25 .42 1 445 .84 26 .42 1 446 .66 27 .42 1 447 .82 28 .42 1 448 .71 29 .42 1 449 1.38 30 .42 1 450 2.28 31 .42 1 451 .86 32 .42 1 452 .66 33 .42 1 453 .96 34 .42 1 454 .75 35 .42 1 455 .76 36 .42 1 456 .77 37 .42 1 457 .78 38 .42 1 458 1.03 39 .42 1 459 .39 40 .405 1 460 .43 41 .42 1 461 .34 42 .39 1 462 .58 43 .39 1 463 -.43 44 .34 1 464 -.55 45 .18 1 465 -.23 46 -.23 1 466 1.27 47 -.23 1 467 1.05 48 -.23 1 468 1.38 49 -.23 1 469 .75 50 -.23 1 470 1.53 51 -.23 1 471 1.61 52 -.23 1 472 .51 53 -.23 1 473 .53 54 -.23 1 474 .71 55 -.23 1 475 .14 56 -.23 1 476 .4 57 -.23 1 477 .45 58 -.23 1 478 .89 59 -.23 1 479 1.53 60 -.045 1 480 .9 60 -.045 1 481 1.98 60 -.045 1 482 .21 60 -.045 1 483 -.09 60 -.16 1 484 .1 60 -.16 1 485 1.01 60 -.16 1 486 .67 60 -.16 1 487 .48 60 -.16 1 488 1 60 -.16 1 489 .28 60 -.16 1 490 .05 60 -.16 1 491 .5 60 -.16 1 492 1.94 60 -.16 1 493 1.44 60 -.16 1 494 1.64 60 -.16 1 495 1.36 60 -.16 1 496 -.31 60 -.27 1 497 -.07 60 -.27 1 498 .85 60 -.27 1 499 1.53 60 -.27 1 500 .4 60 -.27 1 501 .92 60 -.27 1 502 .58 60 -.27 1 503 .21 60 -.27 1 504 .91 60 -.27 1 505 -.31 60 -.31 1 506 .56 60 -.31 1 507 .75 60 -.31 1 508 -.13 60 -.31 1 509 .32 60 -.31 1 510 -1.6 60 -.37 1 511 -.77 60 -.49 1 512 1.57 60 -.49 1 513 .77 60 -.49 1 514 2 60 -.49 1 515 2.02 60 -.49 1 516 3.45 60 -.49 1 517 1.02 60 -.49 1 518 1.09 60 -.49 1 519 1.22 60 -.49 end format %tm mdate
Comment