Announcement

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

  • How to generate exact maximum value with high figures

    Dear Stata users,

    I have a database with import values of format %10.0g (double storage type). Since I have duplicate observations for some countries but with different values, I am first summing them
    Code:
    bysort prod year importer : replace imports=sum(imports) if exporter == "DEU"
    Then, I would like to keep the maximum value and keep only observations where values of imports and imports2 are matching.
    Code:
    bysort year importer exporter prod : egen imports2 = max(imports)
    format imports2 %10.0g
    keep if(imports == imports2)
    However, the maximum value generated does not always match with the variable imports even if there is only one value.
    For instance, the value for the variable imports for one observation could be 22224567 and the maximum value (imports2) is 22224568. So the figure is different only for the last figure even though I have changed the format of the maximum variable generated to match with the format of the initial variable.
    And therefore the last line of my code eliminates many observations that should be kept.

    Could you help me to fixe this ?
    Many thanks.

  • #2
    Perhaps I misunderstand, but it sounds like -collapse-, using the sum function there, might be a better solution for what you want. (My sense is that you want to keep just one entry among certain duplicates, with that entry containing the sum for those duplicates.)

    Leaving that aside, and presuming that all of your code is doing what you want except for the -keep if (imports == imports2)-, what about defining the condition for -keep- by some relative criterion of "closeness?"
    Code:
    keep if (abs(imports - imports2)/imports < 1e-6)

    Comment


    • #3
      Thank you Mike for your reply, I found the solution consisting in generating the new variable imports 2 using command egen double.

      Comment

      Working...
      X