I have the following dataset. And I come up with the following code:
Code:
egen t=max(dateday_count) forvalues i=1/762{ gen k_`i'=. gen ref_`i'=. } levelsof dateday_count, local(levels) foreach i of local levels { di(`i') quietly{ forvalue x=1/`i'{ bys permno (dateday): replace k_`x'=adjvol[_n-`x']*exp(total_log_rev_turnover-total_log_rev_turnover[_n-`x'+1]) if dateday_count==`i' by permno: replace ref_`x' = adjprc[_n-`x']*k_`x' if dateday_count==`i' } } }
In words, what I am trying to do is, looping over the value of variable dateday_count, and for each row, I want to do
Code:
forvalue x=1/"dateday_count"{ bys permno (dateday): replace k_`x'=adjvol[_n-`x']*exp(total_log_rev_turnover-total_log_rev_turnover[_n-`x'+1]) by permno: replace ref_`x' = adjprc[_n-`x']*k_`x' }
Code:
forvalue x=1/12{ bys permno (dateday): replace k_`x'=adjvol[_n-`x']*exp(total_log_rev_turnover-total_log_rev_turnover[_n-`x'+1]) by permno: replace ref_`x' = adjprc[_n-`x']*k_`x' }
In the dataex, I only attached the sample from single permno, you need to remove the "bys permno" part of my code.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input double(permno dateday) float(adjvol adjprc total_log_rev_turnover) double dateday_count 10001 10595 0 2.1041667 0 1 10001 10596 7200 2.0833333 0 2 10001 10597 6150 2.1666667 0 3 10001 10598 2100 2.0416667 -1.2086598 4 10001 10601 600 2.1666667 -1.4323044 5 10001 10602 17250 2.1666667 -1.4323044 6 10001 10603 1500 2.0833333 -2.1274576 7 10001 10604 330 2.0833333 -2.244239 8 10001 10605 1500 2.1666667 -2.939392 9 10001 10608 300 2.1666667 -3.0449755 10 10001 10609 3000 2.0833333 -3.0449755 11 10001 10610 1500 2.1666667 -3.740129 12 10001 10611 0 2.125 -3.740129 13 10001 10612 1500 2.0833333 -4.435282 14 10001 10615 600 2.1666667 -4.658927 15 10001 10616 2982 2.0833333 -10.17839 16 10001 10617 390 2.0833333 -10.317952 17 10001 10618 3300 2.1666667 -10.317952 18 10001 10619 0 2.125 -10.317952 19 10001 10622 17700 2.125 -10.317952 20 10001 10623 7575 2.1666667 -10.317952 21 10001 10624 600 2.1666667 -.2236447 22 10001 10625 6900 2.1666667 -.2236447 23 10001 10626 13500 2.1666667 -.2236447 24 10001 10629 17100 2.1666667 -.2236447 25 10001 10630 5400 2.0833333 -.2236447 26 10001 10631 16200 2.125 -.2236447 27 10001 10632 6000 2.125 -.2236447 28 10001 10633 0 2.125 -.2236447 29 10001 10636 3600 2.0833333 -.2236447 30 10001 10637 3210 2.1666667 -.2236447 31 10001 10638 2100 2.1666667 -1.4323044 32 10001 10639 10080 2.1666667 -1.4323044 33 10001 10640 12816 2.125 -1.4323044 34 10001 10644 8400 2.1666667 -1.4323044 35 10001 10645 12000 2.1666667 -1.4323044 36 10001 10646 0 2.2083333 -1.4323044 37 10001 10647 3000 2.1666667 -1.4323044 38 10001 10650 900 2.25 -1.7898387 39 10001 10651 3300 2.25 -1.7898387 40 10001 10652 3900 2.25 0 41 10001 10653 1200 2.125 -.5121625 42 10001 10654 1350 2.25 -1.1116405 43 10001 10657 3000 2.25 -1.1116405 44 10001 10658 11400 2.25 -1.1116405 45 10001 10659 900 2.25 -1.4691747 46 10001 10660 7800 2.25 -1.4691747 47 10001 10661 450 2.2916667 -1.6320474 48 10001 10664 3600 2.1666667 -1.6320474 49 10001 10665 2550 2.1666667 -3.5405884 50 10001 10666 300 2.2916667 -3.6461716 51 10001 10667 4800 2.2916667 -3.6461716 52 10001 10668 1200 2.2083333 -4.1583343 53 10001 10671 4800 2.2083333 -4.1583343 54 10001 10672 13500 2.2083333 -4.1583343 55 10001 10673 5160 2.3333333 -4.1583343 56 10001 10674 2700 2.25 -6.47912 57 10001 10678 1500 2.3333333 -7.174273 58 10001 10679 600 2.3333333 -7.397918 59 10001 10680 0 2.2916667 -7.397918 60 10001 10681 0 2.2916667 -7.397918 61 10001 10682 1500 2.25 -8.093071 62 10001 10685 0 2.2916667 0 63 10001 10686 4950 2.3333333 0 64 10001 10687 900 2.3333333 -.3575342 65 10001 10688 5400 2.3333333 -.3575342 66 10001 10689 1050 2.25 -.7893968 67 10001 10692 4650 2.3333333 -.7893968 68 10001 10693 5400 2.25 -.7893968 69 10001 10694 1500 2.25 -1.48455 70 10001 10695 3900 2.25 -1.48455 71 10001 10696 10800 2.416667 -1.48455 72 10001 10699 9000 2.416667 -1.48455 73 10001 10700 5940 2.2916667 -1.48455 74 10001 10701 1980 2.2916667 -2.5672574 75 10001 10702 1500 2.416667 -3.2624106 76 10001 10703 1500 2.416667 -3.957564 77 10001 10706 2100 2.416667 -5.166224 78 10001 10707 5400 2.5 -5.166224 79 10001 10708 2400 2.5 -6.78371 80 10001 10709 1500 2.5 -7.478863 81 10001 10710 23250 2.416667 -7.478863 82 10001 10713 14910 2.375 0 83 10001 10714 7800 2.416667 0 84 10001 10715 84111 2.5 0 85 10001 10716 19350 2.416667 0 86 10001 10717 0 2.458333 0 87 10001 10720 8550 2.5 0 88 10001 10721 3900 2.416667 0 89 10001 10722 17499 2.375 0 90 10001 10723 900 2.375 -.3575342 91 10001 10724 3000 2.416667 -.3575342 92 10001 10727 900 2.5 -.7150685 93 10001 10728 2700 2.458333 -3.035854 94 10001 10729 0 2.395833 -3.035854 95 10001 10730 3900 2.3333333 -3.035854 96 10001 10731 600 2.416667 -3.259499 97 10001 10734 0 2.375 -3.259499 98 10001 10735 9600 2.3333333 -3.259499 99 10001 10736 600 2.416667 -3.483144 100 end format %d dateday
0 Response to Looping Over the numeric value of a variable and use it for the subsequent loop.
Post a Comment