Announcement

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

  • How to simply add and divide?

    Dear Statalist users,

    I want to calculate a ratio by simply adding two variables, and then dividing by another variable. I have read chapter 13 of the Stata manual that explains the arithmetic operators, but I cannot seem to accomplish what I want. Let me give you an example:

    Code:
    input double(debtshort debtlong assets)
     195.388    944.9  1923.516
      78.829  281.679   873.659
     666.198 6413.882 12266.546
       134.6    193.8    2486.5
    end
    What I want to accomplish in terms of mathematical/calculation notation is (debtshort + debtlong) / assets. I know how to divide in Stata, but adding variables with the '+' sign does not seem to work. Can anyone share their expertise?

    Thank you

  • #2
    Code:
    gen wanted = (debtshort + debtlong) / assets
    is what you want, modulo your choice of new name. We can't comment on "does not seem to work" as you don't show the code you tried or what happened or why you think that is so.
    Last edited by Nick Cox; 14 Mar 2021, 06:32.

    Comment


    • #3
      Wow, so simple. Thank you, Nick!

      Comment


      • #4
        Hi Nick Cox , please help. I am doing a simple addition from two numeric variables but the result is one more than what it should be in each case. For example, 4+3 is yielding 8, instead of 7. The variables were initially string from an excel file but I encoded in stata before using .gen z = x + y
        Last edited by Michael Olu; 06 May 2022, 15:14.

        Comment


        • #5
          Hi Nick Cox, please help. I am doing a simple addition from two numeric variables but the result is one more than what it should be in each case. For example, 4+3 is yielding 8, instead of 7. The variables were initially string from an excel file but I encoded in stata before using .gen z = x + y

          Comment


          • #6
            Okay. I just discovered that this problem only exists when I am viewing the output window in Stata. When I export a list of the variables of interest with asdoc, the correct math (result) is miraculously displayed.

            Comment


            • #7
              The variables were initially string from an excel file but I encoded in stata before using .gen z = x + y
              That is where you went wrong, and you are lucky that the damage was as limited as it was.

              If you have what should be a numeric variable that is stored as a string in Stata, you should never -encode- it. You have to -destring- it. The two processes are completely different. -encode- does not extract the numerical values of the string: it assigns new ones, starting from 1 and increasing. -destring- actually figures out the numeric value that the string represents and creates a numeric variable containing that (or replace the original string variable with a numeric variable of the same name and containing the numeric values.)

              The reason it seems better with -asdoc-, I do not know. It may be that -asdoc- is showing you the "value labels" created by -encode- rather than the actual numeric values that are being stored. In any case, I assure you that these -encode-d variables are wrong. Eliminate them now and use -destring- to create correct numeric variables before you get into more trouble.

              (In case you are wondering what -encode- is for, it is for taking string variables with values like "Male" and "Female" and creating a corresponding numeric variable with values like 1 representing female and 2 representing male--such variables can be used for analysis purposes. But it should never be applied to a string variable whose values look like numbers: that will always lead to trouble.)

              Comment


              • #8
                @Thank you very much for that very thorough explanation. I will use -destring when I revisit this data and I will let you know how it goes (next week). This is enlightening. I have been using encode and destring interchangeably, but that will never happen again.

                Comment

                Working...
                X