Announcement

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

  • Help generating new variable conditional on another row

    I am trying to create a variable/column that has the same value everywhere and that value is equal to the value of the variable percentcumulativemktvalue for the row where rank==1000.

    So I tried
    Code:
    generate newvariable=percentcumulatemktvalue if rank==1000
    But that creates a new column where all the values are missing except for the row where rank==1000. I want that value to be everywhere in the new column. How should I do that? I am sorry if its a silly question but I am a beginner with stata. help egen and help generate didnt get me much info. Thanks.
    My data is as below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double PERMNO long date double(PERMCO PRC SHROUT CFACPR CFACSHR mn yr dy mktvalue rank cumulativemktvalue totalmktvalue percentcumulativemktvalue)
    11850 17317 20678  83.16999816894531  5633270        1       1 5 2007 31 468519055.58517456   1 468519055.58517456 17690235874.438152  2.648461325844559
    12060 17317 20792  37.58000183105469 10288157     .125    .125 5 2007 31  386628958.8981781   2  855148014.4833527 17690235874.438152  4.834011375275189
    10107 17317  8048 30.690099716186523  9566808        1       1 5 2007 31 293606291.48561096   3 1148754305.9689636 17690235874.438152  6.493719553106006
    70519 17317 20483   54.4900016784668  4946439       .1      .1 5 2007 31  269531469.4124336   4 1418285775.3813972 17690235874.438152  8.017336713021315
    66093 17317 21645  41.34000015258789  6165556 1.305385       1 5 2007 31 254884085.98078918   5 1673169861.3621864 17690235874.438152  9.458154618389603
    59408 17317  3151 50.709999084472656  4437771        1       1 5 2007 31  225039363.3470993   6 1898209224.7092857 17690235874.438152 10.730265204954897
    18163 17317 21446  63.54999923706055  3148924        1       1 5 2007 31 200114117.79756165   7 2098323342.5068474 17690235874.438152 11.861477469268007
    55976 17317 21880 47.599998474121094  4121669        1       1 5 2007 31  196191438.1108322   8 2294514780.6176796 17690235874.438152  12.97051546911923
    21936 17317 21394 27.489999771118164  7018263 1.056256       1 5 2007 31 192932048.26364708   9 2487446828.8813267 17690235874.438152 14.061128673109502
    66800 17317   137  72.33999633789063  2594237   .05975     .05 5 2007 31 187667095.07962036  10  2675113923.960947 17690235874.438152 15.121979960857415
    22111 17317 21018  63.27000045776367  2896558        1       1 5 2007 31 183265225.98593903  11  2858379149.946886 17690235874.438152 16.157948205072586
    47896 17317 20436  51.83000183105469  3416115        1       1 5 2007 31 177057246.70509338  12 3035436396.6519794 17690235874.438152 17.158823761293608
    14541 17317 20440  81.48999786376953  2149237        1       1 5 2007 31 175141318.53873444  13  3210577715.190714 17690235874.438152  18.14886889004064
    76076 17317 10486 26.920000076293945  6071255        1       1 5 2007 31     163438185.0632  14  3374015900.253914 17690235874.438152  19.07275812601947
    12490 17317 20990  106.5999984741211  1484827 1.043657       1 5 2007 31  158282555.9343338  15 3532298456.1882477 17690235874.438152 19.967503436697022
    13901 17317 21398   71.0999984741211  2103246 3.278378       1 5 2007 31  149540787.3907013  16  3681839243.578949 17690235874.438152 20.812832964534373
    59328 17317  2367 22.179899215698242  5810000        1       1 5 2007 31 128865214.44320679  17  3810704458.022156 17690235874.438152 21.541286871864195
    13928 17317 21401  77.43000030517578  1634400  1.28986       1 5 2007 31  126551592.4987793  18  3937256050.520935 17690235874.438152  22.25666225406383
    65875 17317 20288 43.529998779296875  2903364 1.069983       1 5 2007 31 126383431.37585449  19 4063639481.8967896 17690235874.438152 22.971087049034907
    11308 17317 20468   52.9900016784668  2309870        2       2 5 2007 31  122400015.1770401  20 4186039497.0738297 17690235874.438152  23.66299424600962
    27828 17317 20908 45.709999084472656  2676447 2.047722       1 5 2007 31 122340389.91963959  21  4308379886.993469 17690235874.438152 24.354564391190202
    38703 17317 21305  36.09000015258789  3339747        1       1 5 2007 31 120531469.73960495  22  4428911356.733074 17690235874.438152  25.03590900748087
    17778 17317   540             109490     1087        1       1 5 2007 31          119015630  23  4547926986.733074 17690235874.438152 25.708684830509743
    90319 17317 45483 497.90899658203125   231472   39.938      20 5 2007 31 115251991.25683594  24   4663178977.98991 17690235874.438152 26.360185421428216
    22752 17317 21188  52.45000076293945  2167273 1.050061       1 5 2007 31 113673470.50349808  25  4776852448.493408 17690235874.438152  27.00276289360174
    13856 17317 21384  68.33000183105469  1628923        1       1 5 2007 31  111304311.5726471  26  4888156760.066055 17690235874.438152 27.631947899175792
    14593 17317     7 121.19100189208984   864948       28      28 5 2007 31 104823914.70455933  27  4992980674.770615 17690235874.438152 28.224500284845373
    36469 17317  1869 54.189998626708984  1913353        1       1 5 2007 31 103684596.44240952  28  5096665271.213024 17690235874.438152 28.810612291368876
    10104 17317  8045   19.3799991607666  5107000        1       1 5 2007 31  98973655.71403503  29  5195638926.927059 17690235874.438152 29.370094123134887
    86868 17317 35048 230.82000732421875   408470        1       1 5 2007 31  94283048.39172363  30  5289921975.318783 17690235874.438152 29.903060721550684
    69032 17317 21224  85.04000091552734  1053931 1.192766       1 5 2007 31  89626293.20490265  31  5379548268.523685 17690235874.438152 30.409703447182224
    20482 17317 20017 56.349998474121094  1540360 2.095788       1 5 2007 31  86799283.64959717  32  5466347552.173283 17690235874.438152  30.90036555177869
    87031 17317  1928   79.7699966430664  1053170        1       1 5 2007 31  84011367.36457825  33  5550358919.537861 17690235874.438152  31.37526802318085
    52919 17317 21190   92.7300033569336   868179        1       1 5 2007 31  80506241.58441925  34   5630865161.12228 17690235874.438152  31.83035659382421
    77418 17317 11412   21.3700008392334  3766683  .468922  .33333 5 2007 31  80494018.87112617  35  5711359179.993406 17690235874.438152  32.28537607147537
    19561 17317 20315 100.58999633789063   787401        1       1 5 2007 31  79204663.70645142  36  5790563843.699858 17690235874.438152  32.73310703599518
    15667 17317 20086  57.84000015258789  1345160        1       1 5 2007 31  77804054.60525513  37  5868367898.305113 17690235874.438152 33.172920587139956
    59176 17317    90   64.9800033569336  1187588        1       1 5 2007 31  77169472.22665405  38  5945537370.531767 17690235874.438152  33.60914694824893
    66181 17317  5085 38.869998931884766  1969535        1       1 5 2007 31  76555823.34630966  39  6022093193.878077 17690235874.438152  34.04190445295202
    92655 17317  7267  54.77000045776367  1340049        1       1 5 2007 31  73394484.34342575  40  6095487678.221502 17690235874.438152 34.456791427124465
    77178 17317 11253  42.95000076293945  1666766        1       1 5 2007 31  71587600.97164154  41  6167075279.193144 17690235874.438152 34.861464386149756
    26403 17317 20587 35.439998626708984  1981629 1.014118       1 5 2007 31   70228929.0386467  42  6237304208.231791 17690235874.438152  35.25845699572895
    17830 17317 21832  70.55000305175781   994430 1.812738       1 5 2007 31  70157039.53475952  43   6307461247.76655 17690235874.438152  35.65504322574149
    50876 17317 21102 58.619998931884766  1134043        1       1 5 2007 31 66477599.448711395  44  6373938847.215261 17690235874.438152  36.03083018483325
    14008 17317   216  56.45000076293945  1159645        1       1 5 2007 31  65461961.13473892  45      6439400808.35 17690235874.438152  36.40087590722709
    39087 17317 21833 22.850000381469727  2813799        1       1 5 2007 31 64295308.223379135  46 6503696116.5733795 17690235874.438152  36.76432673218915
    22592 17317 21205  87.95999908447266   720205        1       1 5 2007 31  63349231.14063263  47  6567045347.714012 17690235874.438152 37.122429538676705
    51043 17317 20695  63.91999816894531   973047        1       1 5 2007 31  62197162.45829773  48   6629242510.17231 17690235874.438152  37.47401988998554
    60097 17317  2850  53.16999816894531  1149021        1       1 5 2007 31  61093444.46607971  49   6690335954.63839 17690235874.438152  37.81937110463134
    43449 17317 21177  50.54999923706055  1193876        1       1 5 2007 31   60350430.8891449  50 6750686385.5275345 17690235874.438152  38.16052218547334
    17005 17317 21185 38.540000915527344  1564130        1       1 5 2007 31 60281571.632003784  51  6810967957.159538 17690235874.438152   38.5012840162362
    66157 17317  1645  34.58000183105469  1738724        1       1 5 2007 31  60125079.10369873  52  6871093036.263237 17690235874.438152  38.84116122042079
    11081 17317  9833 26.910999298095703  2231000        1       1 5 2007 31  60038439.43405151  53 6931131475.6972885 17690235874.438152  39.18054866477253
    19393 17317 20331 30.309999465942383  1968065        1       1 5 2007 31 59652049.098939896  54  6990783524.796228 17690235874.438152  39.51775190797594
    89525 17317 43613  27.40999984741211  2068612        2       2 5 2007 31  56700654.60435486  55  7047484179.400583 17690235874.438152 39.838271402497135
    89006 17317 41680  33.84000015258789  1604112 1.540555       1 5 2007 31  54283150.32476807  56  7101767329.725351 17690235874.438152   40.1451251420971
    49154 17317 20561  62.43000030517578   855321        1       1 5 2007 31 53397690.291023254  57  7155165020.016375 17690235874.438152 40.446973521451845
    21776 17317 21396                 78   672650 1.412272       1 5 2007 31           52466700  58  7207631720.016375 17690235874.438152  40.74355916548959
    15579 17317 21737  35.36000061035156  1433709        1       1 5 2007 31  50695951.11506653  59  7258327671.131441 17690235874.438152 41.030135056703806
    87842 17317 37138                 68   741607 1.115617       1 5 2007 31           50429276  60  7308756947.131441 17690235874.438152  41.31520347726042
    18542 17317 20408  78.58000183105469   640396        1       1 5 2007 31   50322318.8526001  61  7359079265.984041 17690235874.438152  41.59966728661705
    89179 17317 42273  81.41000366210938   613655        1       1 5 2007 31  49957655.79727173  62  7409036921.781313 17690235874.438152  41.88206971557199
    83443 17317   540               3625    13674       50      50 5 2007 31           49568250  63  7458605171.781313 17690235874.438152  42.16227089746592
    61399 17317  2709  32.81999969482422  1506000        1       1 5 2007 31  49426919.54040527  64  7508032091.321718 17690235874.438152  42.44167316146753
    25013 17317 21575   32.7400016784668  1489389        1       1 5 2007 31 48762598.359889984  65  7556794689.681608 17690235874.438152  42.71732012686696
    87447 17317 36383  71.97000122070313   677238        1       1 5 2007 31  48740819.68670654  66  7605535509.368315 17690235874.438152  42.99284398100129
    11703 17317 20606  52.31999969482422   923674 1.053752       1 5 2007 31 48326623.398117065  67  7653862132.766432 17690235874.438152  43.26602645149592
    90441 17317 21287  22.09000015258789  2161056 1.125765       1 5 2007 31  47737727.36975098  68  7701599860.136183 17690235874.438152  43.53587998939437
    89258 17317 42524  102.0199966430664   463000        1       1 5 2007 31 47235258.445739746  69  7748835118.581923 17690235874.438152  43.80289315293275
    34833 17317 21322 54.970001220703125   833814 1.037537       1 5 2007 31 45834756.597839355  70  7794669875.179762 17690235874.438152  44.06198950938139
    10145 17317 22168  57.90999984741211   779718 1.047784       1 5 2007 31 45153469.261024475  71  7839823344.440786 17690235874.438152  44.31723466033085
    19502 17317 21881 45.130001068115234   997437        1       1 5 2007 31 45014332.875377655  72  7884837677.316164 17690235874.438152 44.571693296127904
    86356 17317 16285 32.560001373291016  1363838 2.416521       1 5 2007 31  44406567.15294647  73 7929244244.4691105 17690235874.438152  44.82271633204521
    75789 17317 22096  66.79000091552734   661540        1       1 5 2007 31  44184257.20565796  74  7973428501.674768 17690235874.438152  45.07248268631696
    20626 17317 20597 45.380001068115234   953241        1       1 5 2007 31 43258077.598171234  75   8016686579.27294 17690235874.438152  45.31701349927621
    15069 17317 21795 123.80999755859375   342978 3.280728       2 5 2007 31  42464105.34265137  76  8059150684.615591 17690235874.438152 45.557056117385166
    22779 17317 21230 18.190000534057617  2314619  .246956 .142857 5 2007 31  42102920.84613991  77  8101253605.461731 17690235874.438152 45.795057018814504
    21178 17317 21110   98.0999984741211   419624        1       1 5 2007 31  41165113.75970459  78  8142418719.221436 17690235874.438152  46.02775665070119
    85269 17317 32066  74.62000274658203   548991 1.090979       1 5 2007 31 40965709.927848816  79  8183384429.149284 17690235874.438152  46.25932908545343
    59184 17317    29  53.34000015258789   759798        1       1 5 2007 31 40527625.435935974  80   8223912054.58522 17690235874.438152 46.488425100473194
    22293 17317 20523                 25  1572789        1       1 5 2007 31           39319725  81   8263231779.58522 17690235874.438152 46.710693052574484
    80599 17317 21606  73.37999725341797   532618        1       1 5 2007 31  39083507.37712097  82  8302315286.962341 17690235874.438152 46.931625705222686
    77274 17317 11300   82.7699966430664   466917        4       4 5 2007 31  38646718.52259064  83  8340962005.484932 17690235874.438152  47.15008926216392
    81593 17317  5849 43.720001220703125   882408  .083333 .083333 5 2007 31   38578878.8371582  84   8379540884.32209 17690235874.438152  47.36816933249754
    83435 17317 14521 28.700000762939453  1343854        1       1 5 2007 31 38568610.825279236  85  8418109495.147369 17690235874.438152  47.58619135944524
    22103 17317 20643  48.45000076293945   795263        1       1 5 2007 31  38530492.95673752  86  8456639988.104107 17690235874.438152  47.80399791233814
    79323 17317 29870               61.5   607408        1       1 5 2007 31           37355592  87  8493995580.104107 17690235874.438152  48.01516294295244
    27887 17317 20277  56.84000015258789   651517 1.810602       1 5 2007 31 37032226.379413605  88 8531027806.4835205 17690235874.438152  48.22450004078574
    59459 17317  4293  54.16999816894531   663999        1       1 5 2007 31  35968824.61418152  89  8566996631.097702 17690235874.438152  48.42782590297029
    24643 17317 20060 41.279998779296875   869524  .444444 .444444 5 2007 31 35893949.658569336  90  8602890580.756271 17690235874.438152  48.63072850931957
    10147 17317  8093 16.889999389648438  2098455        1       1 5 2007 31  35442903.66920471  91  8638333484.425476 17690235874.438152  48.83108142671858
    91883 17317 51978  38.40999984741211   901913  .333333 .333333 5 2007 31    34642478.192379  92  8672975962.617855 17690235874.438152 49.026909670266406
    60628 17317  1685 111.62000274658203   307801        1       1 5 2007 31 34356748.465400696  93  8707332711.083256 17690235874.438152  49.22112273056282
    18729 17317 20473  66.95999908447266   511492        2       2 5 2007 31  34249503.85171509  94   8741582214.93497 17690235874.438152  49.41472955465952
    87137 17317 35222  76.77999877929688   444981        1       1 5 2007 31   34165640.6368103  95  8775747855.571781 17690235874.438152  49.60786231376636
    37584 17317  6407 135.74000549316406   249631        3       3 5 2007 31  33884913.31126404  96  8809632768.883045 17690235874.438152  49.79940816737607
    88668 17317 40148 61.599998474121094   544256        1       1 5 2007 31  33526168.76953125  97  8843158937.652576 17690235874.438152 49.988926096970076
    50227 17317 20350  93.12999725341797   356117        1       1 5 2007 31 33165175.231895447  98  8876324112.884472 17690235874.438152  50.17640338934365
    81055 17317 30513  79.77999877929688   415593        1       1 5 2007 31 33156009.032684326  99  8909480121.917156 17690235874.438152  50.36382886669748
    23819 17317 20868  35.95000076293945   914051        1       1 5 2007 31  32860134.14736557 100  8942340256.064522 17690235874.438152  50.54958181188488
    end
    format %td date

  • #2
    That's a pretty unhelpful data example: it doesn't have any observation where rank == 1000. Nevertheless, without testing, in a data set that does contain one, and only one, observation where rank == 1000, the following should work:
    Code:
    egen wanted = max(cond(rank == 1000, percentcumulativemktvalue, .))

    Comment


    • #3
      Thanks a lot Clyde!! It works.
      After searching google, I came across your post on stack exchange, that also helped. I tried the following
      Code:
      egen centralcutoff= total(percentcumulativemktvalue*(rank== 1000))
      But I assume mine also only works when there is a single rank==1000 value, which is true for the dataset.

      Just a quick follow up question, how can I arrange my dataset to be more helpful when posting here, I see it has occured with me a few times.
      I can try using sort gsort +- on a variable but none of it will ever arrage the dataset such that when I do dataex, I get a variable like rank==1000 in the first 100 observations.

      Comment


      • #4
        After searching google, I came across your post on stack exchange, that also helped.
        I have never posted on Stack Exchange. Perhaps you are confusing me with Nick Cox?

        Just a quick follow up question, how can I arrange my dataset to be more helpful when posting here, I see it has occured with me a few times.
        If you just run -dataex-, you will get, by default, the first 100 observations in the data set. If you sort it different ways, those can be different. But sorting is by no means the only way to pull different data sets out with -dataex-. Like so many Stata commands, -dataex- allows -if- and -in- conditions. These are flexible enough to identify just about any subset of the data you can imagine.

        So, you have to think about the critical aspects of the data set. In this instance, the key is to have some observation with rank == 1000, and some others where it isn't 1000. One way to do that would be -dataex if inrange(rank, 975, 1025)-. I'm sure you can think of other conditions on the value rank that include 1000 and some other numbers. Similar considerations apply to other problems where some special values of a variable are central. E.g. -dataex if inlist(mn, 5, 6)-.

        I think most posts involve problems with some code that isn't preforming as hoped. In that case, the first thing is to make sure that every variable that is mentioned in the code is part of the -dataex- output. Once you've done that, run the -dataex- output yourself and then run the code that you are concerned with: the code should run and produce all and only the error messages the code produced with the original data. And, if the original data produced some results with the code, the results with the example should be similar (not necessarily identical) to the original results. To do this you may have to use some compound -if- conditions like -if inlist(id, 1, 2, 3, 4) & inrange(time, 1, 10)- in a panel data set to be sure you have several panels and enough of the time observations within each of the panels to provide a few observations that have enough lags or leads to run the regression that is giving problems.

        Comment

        Working...
        X