Announcement

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

  • Generating a new variable as the mean of multiple variables

    Hi,

    I want to create a new variable called mean wage which is going to measure the average wage by job. I have 2 wage variables in my data. One for the employees and one for the self-employed. I have been trying different combinations of the egen command like the below however they are obviously wrong and I am not sure how to correct them or if I shall be using a different command after all
    Code:
    egen meanwage= mean (employeewage)if employmentstatus==3 & mean (selfemployedwage)if employmnetstatus<3, by (job)
    I know that for a single mean variable the code would be:
    Code:
    egen meanwage= mean (employeewage), by (job)
    however I am confused as to how to combine 2 mean variables into 1
    Last edited by Christina Chara; 06 Dec 2016, 08:44.

  • #2
    Code:
    egen mean = mean(cond(employmentstatus == 3, selfemployed, employeewage)), by(job)
    The egen function mean() will take expressions as arguments. Otherwise put, the branching must take place within the argument to mean(). Anything else would not be consistent with the declared command syntax.

    egen [type] newvar = fcn(arguments) [if] [in] [, options]

    Comment


    • #3
      Thank you,
      there was a mistake in my code earlier-i have now edited the original post (it should be selfemployedwage rather than selfemployed).
      In the code you provided, you list the first condition for the mean that is that someone is an employee (i.e employmentstatus==3) but not the second one i.e. that someone is self-employed (employmentstatus<3). Is there a reason for this?

      Comment


      • #4
        That's all you need to specify unless employmentstatus can be above 3.

        Even if it can then only the variables

        Code:
        selfemployed employeewage
        are used and for consistency they should be missing if there are some other categories.

        If say, there is another category (employer? unemployed?), you need to tell us more about the rules and I will suggest better code.

        For the moment, this ignores other categories explicitly.

        Code:
        local es "employmentstatus"  
        egen mean = mean(cond(`es' == 3, selfemployedwage, cond(`es' < 3, employeewage, .))), by(job)
        I am not an economist and claim ignorance of economic nuances unless instructed otherwise.

        Comment


        • #5
          Thank you very much-it all makes sense and it works for what I want to do

          Comment


          • #6
            Originally posted by Nick Cox View Post
            That's all you need to specify unless employmentstatus can be above 3.

            Even if it can then only the variables

            Code:
            selfemployed employeewage
            are used and for consistency they should be missing if there are some other categories.

            If say, there is another category (employer? unemployed?), you need to tell us more about the rules and I will suggest better code.

            For the moment, this ignores other categories explicitly.

            Code:
            local es "employmentstatus"
            egen mean = mean(cond(`es' == 3, selfemployedwage, cond(`es' < 3, employeewage, .))), by(job)
            I am not an economist and claim ignorance of economic nuances unless instructed otherwise.
            Dear Nick Cox,

            I have a question regarding how to generate a new variable as a combination of means of 20 other variables, say x1-x20. In fact, I want co create a continuous variable, in which its first value is the mean of x1, second value is the mean of x2 and so on. I have tried some egen command but it did not work. Do you have any idea how to solve my issue? Following is an example of my data.

            Code:
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(pfitfm0 pfitfm1 pfitfm2 pfitfm3 pfitfm4 pfitfm5 pfitfm6 pfitfm7 pfitfm8 pfitfm9 pfitfm10 pfitfm11 pfitfm12 pfitfm13 pfitfm14 pfitfm15 pfitfm16 pfitfm17 pfitfm18 pfitfm19 pfitfm20)
            .10463551 .18859567 .17903563 .12113285   .0700939 .04168195  .02937712  .02450828 .022226144 .020640487  .01918993 .017757878  .01634406  .014968617  .013649706  .012400028  .011227395  .010135796  .009126367  .008198163   .007348758
            .10674594 .19206114  .1836562  .1260093 .074436665   .045195 .032063246  .02645224 .023514783 .021353874 .019405574 .017551947 .015789872  .014134712  .012598407  .011186708  .009900246  .008735988  .007688461  .006750648   .005914662
            .14631665 .22727574 .19234304 .12168118  .07094832 .04529748 .033570144 .027361417  .02305074 .019496227 .016411878 .013730334 .011421092  .009452714  .007789677   .00639498  .005232575  .004268885  .003473558  .002819768   .002284182
            .12738018 .21875027  .2140069 .15357025  .09400337 .05592218  .03560556 .024859926  .01844593  .01402756 .010717482 .008165532 .006191996  .004673286  .003512035  .002629414 .0019620825 .0014598167 .0010832798 .0008019766  .0005924632
            .11596175  .2068972  .2068765  .1508547  .09381617 .05707933  .03762115 .027465757 .021393294 .017085772 .013704724 .010959052 .008721266  .006907397   .00544739 .0042798175   .00335135  .002616597 .0020375834 .0015829696    .00122718
             .1565635 .23407882  .1912937 .11810946  .06851536 .04425048 .033138297  .02702083 .022620173  .01896021 .015804155  .01308939  .01077827  .008830732  .007203724  .005854291  .004741856  .003829523  .003084624  .002478782   .001987711
            .07259528 .15575953 .18032323 .14732549   .0981221 .06059001  .03947315  .02932053  .02444502   .0216184  .01948727 .017601732 .015846122  .014203521   .01267804  .011274068  .009992384 .0088304095  .007783122  .006843928   .006005317
             .1240795 .21265008   .200156 .13805638  .08337195 .05155048  .03587453  .02783793   .0227942  .01894931 .015744818 .013018537 .010706368  .008761551  .007139072  .005795097  .004688532  .003782122  .003042954  .002442497   .001956371
            .14141308 .22993734  .2092177 .14098105   .0837142 .05071128 .034034688   .0250714  .01933596 .015103242  .01178452 .009149483 .007065365  .005429156 .0041538677 .0031661496   .00240529 .0018219053 .0013764028  .001037394  .0007802267
            .14719559  .2206785 .17677534  .1055158   .0592681 .03836569  .03011743  .02630435 .023697764  .02138194  .01918138 .017095814 .015148457   .01335554  .011723764  .010252484  .008936097  .007765852  .006731186  .005820666   .005022645
            .11469015  .2024765  .1938808 .13490961  .08159777 .05050125 .035567373 .028321164 .023979867  .02066857  .01781734   .0152868  .01304536  .011077855  .009366496  .007889638    .0066236  .005544386   .00462886  .003855436  .0032044344
              .180449 .25629285 .20402105 .12415428 .070719235 .04364121 .030239575  .02245472 .017051762  .01296056 .009797483 .007359704  .00549667 .0040846993  .003022276 .0022277427  .001636638 .0011988443 .0008758586 .0006383868 .00046431494
            .12991333 .22118975  .2147115 .15304828  .09321553 .05528016 .035115633 .024442395 .018057588 .013661424  .01038003 .007863636 .005929031 .0044492274  .003324518 .0024747765 .0018361222  .001358282  .001002166 .0007376795  .0005418445
              .106886 .19481774   .192036 .13667215  .08352373 .05144685  .03585189 .028450014 .024219487  .02108762 .018396199 .015981117 .013810705  .011876825  .010169752  .008675197   .00737574  .006252508  .005286452   .00445916    .00375336
            .16626485 .24805637  .2072587 .13064462  .07522021 .04588848 .031349137  .02317215 .017656038  .01351455  .01030146  .00780571 .005881134  .004409004 .0032910574 .0024473025 .0018138306 .0013403813 .0009879181 .0007264278  .0005330186
            .12335978  .2097462 .19328834  .1303235  .07763296 .04839556 .034726426 .028014855  .02381695  .02050831 .017628195 .015071874   .0128151  .010842258  .009133467  .007664948  .006411205  .005346784 .0044474048 .0036906213  .0030561246
            .19366503 .26260272 .19989116 .11800907 .066885844 .04190012 .029463783 .021992877 .016677145 .012622562 .009493326 .007093105 .005268925 .0038942415 .0028657375 .0021009054 .0015350878 .0011183615   .00081263 .0005890901  .0004261373
            .14430827 .22087073 .18140927 .11092403  .06321512 .04074424 .031439763 .026959617 .023892656  .02123133   .0187655 .016480446 .014389905   .01250155  .010813907  .009318758 .0080036875  .006854007  .005854104  .004988327  .0042415825
             .1649759 .24178454  .1953727  .1200617  .06955566 .04459278  .03275539  .02599388 .021120323 .017170904 .013880768  .01114927 .008903495  .007074444  .005596768  .004411019  .003464952    .0027138 .0021199216 .0016521156  .0012848126
            .08451232 .16858865 .17664875 .12999913  .07887524 .04612721 .030385144 .023945214  .02127933  .01981438  .01863675 .017494963  .01634645   .01520065  .014074638   .01298295  .011936226  .010941663  .010003707  .009124668   .008305235
            end
            Thank you.

            Best regards,

            DL

            Comment

            Working...
            X