Announcement

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

  • Identifying maximum value within observations

    Dear stata users,

    I would like to identify maximum value within an observation (i.e. in a row) and I found relevant post regarding this problem (#10 in https://www.statalist.org/forums/for...value-in-a-row).
    In more detail, within an observation, there are several variables (proba_w*) and within these variables, I would like to find the maximum value.

    Therefore I used the following code:

    gen which_max = ""
    gen max = 0
    unab xvars : proba_w*

    quietly foreach x of local xvars {
    replace which_max = "`x'" if `x' > max
    replace max = `x' if `x' > max
    generate new`x' = 1 if `x' == max
    }


    However, since there are many missing values, all of the outputs were generated as "." (missing value).
    Thus I also tried:
    quietly foreach x of local xvars {
    replace which_max = "`x'" if `x' > max if !missing(`x')
    replace max = `x' if `x' > max if !missing(`x')
    generate new`x' = 1 if `x' == max if !missing(`x')
    }


    But stata tells that it is invalid syntax.

    In this case, is there other solution for this issue ?
    Below is the example of my dataset !


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(proba_w1 proba_w2 proba_w3 proba_w4 proba_w5 proba_w6 proba_w7 proba_w8 proba_w9 proba_w10 proba_w11 proba_w12 proba_w13 proba_w14 proba_w15 proba_w16)
      .03026 .2674339  .080675 .0266589 .0743919 .0532814 .0402332 .0486886 .0588732 .0676515 .1513492 .0613896 .0391136        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0481362 .0875701 .0656627 .0334351 .0620904 .0461794 .6569261        .        .        .        .        .        .        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .1086421 .0518883  .185701  .170221 .0487156 .0279766  .067404 .1202797 .0358585 .0839067  .057714 .0416925        .        . . .
    .0481362 .0875701 .0656627 .0334351 .0620904 .0461794 .6569261        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0285737 .9463882 .0250381        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0435606 .9564394        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0601147 .1752616 .7646238        .        .        .        .        .        .        .        .        .        .        . . .
    .1149132 .6570268 .0486029 .1107815 .0253727 .0433029        .        .        .        .        .        .        .        . . .
    .1275271 .1752543 .0799214 .0487572 .1259108  .080957 .0902294 .0569464  .036991 .0741495 .1033559        .        .        . . .
    .1769857 .0300874  .042601 .0819521 .1508687  .328286 .0350726 .0292512 .1248953        .        .        .        .        . . .
    .0301293 .2381887 .0832349 .0727058 .4165926 .0557396 .1034091        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           1        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0435606 .9564394        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0429105 .1613675 .5397076 .0297093 .2263051        .        .        .        .        .        .        .        .        . . .
    .0322937 .9677063        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0481362 .0875701 .0656627 .0334351 .0620904 .0461794 .6569261        .        .        .        .        .        .        . . .
      .10738 .0253908 .0507688 .0715184 .1325313 .0297062 .1735927 .0360897 .0286385  .041014 .1295937 .0760689 .0293625 .0683445 . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
    .3391361  .096389 .5644749        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
    .0322937 .9677063        .        .        .        .        .        .        .        .        .        .        .        . . .
    .2985279 .0304963 .0659042 .1057932 .0717331 .1876501 .0362286 .0401247 .0618577 .0503969 .0512872        .        .        . . .
     .667057 .0231578 .0233776 .0307358 .1794787 .0451668 .0310263        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
      .10738 .0253908 .0507688 .0715184 .1325313 .0297062 .1735927 .0360897 .0286385  .041014 .1295937 .0760689 .0293625 .0683445 . .
    .1769857 .0300874  .042601 .0819521 .1508687  .328286 .0350726 .0292512 .1248953        .        .        .        .        . . .
    .0523292 .0335115 .1308968 .0268172 .0434962 .5388438 .0307726 .1065412 .0219447 .0148468        .        .        .        . . .
    .0435606 .9564394        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0523292 .0335115 .1308968 .0268172 .0434962 .5388438 .0307726 .1065412 .0219447 .0148468        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0301293 .2381887 .0832349 .0727058 .4165926 .0557396 .1034091        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0429105 .1613675 .5397076 .0297093 .2263051        .        .        .        .        .        .        .        .        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0285737 .9463882 .0250381        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0285737 .9463882 .0250381        .        .        .        .        .        .        .        .        .        .        . . .
    .0481362 .0875701 .0656627 .0334351 .0620904 .0461794 .6569261        .        .        .        .        .        .        . . .
    .0961457 .0556349 .0751973 .1230852 .1336289   .07436 .0707817 .0325561 .0768364 .0646119 .0862089 .0554442 .0555088        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
    .3391361  .096389 .5644749        .        .        .        .        .        .        .        .        .        .        . . .
    .0601147 .1752616 .7646238        .        .        .        .        .        .        .        .        .        .        . . .
    .1234693 .1683535 .0003704 .0374272 .1715978 .0429654 .4558164        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0426214 .1446299 .0238678 .0299127 .0010035 .2953584 .0289108 .4336954        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0961457 .0556349 .0751973 .1230852 .1336289   .07436 .0707817 .0325561 .0768364 .0646119 .0862089 .0554442 .0555088        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
    .8426102 .1573898        .        .        .        .        .        .        .        .        .        .        .        . . .
    .1769857 .0300874  .042601 .0819521 .1508687  .328286 .0350726 .0292512 .1248953        .        .        .        .        . . .
    .0420886 .0248145 .0886628 .0330078 .3742491 .2927863 .0244215 .0644142 .0241817 .0313734        .        .        .        . . .
    .0301293 .2381887 .0832349 .0727058 .4165926 .0557396 .1034091        .        .        .        .        .        .        . . .
    .0301293 .2381887 .0832349 .0727058 .4165926 .0557396 .1034091        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
    .3391361  .096389 .5644749        .        .        .        .        .        .        .        .        .        .        . . .
    .1143764 .0383484 .8472751        .        .        .        .        .        .        .        .        .        .        . . .
    .0285737 .9463882 .0250381        .        .        .        .        .        .        .        .        .        .        . . .
    .0285737 .9463882 .0250381        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
     .596612 .1114243 .0316469 .2603167        .        .        .        .        .        .        .        .        .        . . .
           1        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .2985279 .0304963 .0659042 .1057932 .0717331 .1876501 .0362286 .0401247 .0618577 .0503969 .0512872        .        .        . . .
    .9526188 .0251774 .0222038        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           1        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0322937 .9677063        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0435606 .9564394        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
           .        .        .        .        .        .        .        .        .        .        .        .        .        . . .
    .0758009 .1389739 .6338776  .067394  .056479 .0274745        .        .        .        .        .        .        .        . . .
    .0523292 .0335115 .1308968 .0268172 .0434962 .5388438 .0307726 .1065412 .0219447 .0148468        .        .        .        . . .
     .261457 .1027367 .2432615 .2588521 .1336927        .        .        .        .        .        .        .        .        . . .
    .0675505 .0655442 .0275786 .8393268        .        .        .        .        .        .        .        .        .        . . .
    end

    Thank you in advance,
    AC

  • #2
    The code you are using is not identifying the maximum value, it is identifying the name of the variable where the maximum value is contained.

    The way how you have asked your question "I would like to identify maximum value within an observation (i.e. in a row) " -egen, rowmax- would do the job in one line.

    Comment


    • #3
      Many thanks !

      Comment

      Working...
      X