Announcement

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

  • egen & total()

    Hi,

    I am using egen total() with Stata 10.1.

    The following works correctly but does not store the result in a variable so I can use it:

    total X if stu_id==710740 & hsflag==1

    The following produces missing values:

    egen points=total(X) if stu_id==710740 & hsflag==1

    ​What am I doing wrong?

    Stuart

  • #2
    Nothing is identifiably wrong here except expectations.

    1. total makes no claim or promise to produce a variable. That would not be expected as its mission is to emit various scalar and matrix results. See its help under "Stored results" to see what it leaves behind.

    2. Your new variable created by egen should leave missing values in every observation except those for which stu_id==710740 & hsflag==1.

    I guess you should start with the help for saved results and the corresponding manual entry.

    Also, variable in Stata always means column in the dataset. See e.g.
    http://www.stata.com/statalist/archive/2008-08/msg01258.html
    Last edited by Nick Cox; 25 Jun 2014, 12:36.

    Comment


    • #3
      Perhaps you can better explain what you are trying to do.

      Code:
      total X if stu_id==710740 & hsflag==1
      will produce a single number corresponding to the sum of all X values among those observations with stu_id == 710740 and hsflag == 1. If you need to reference that single number, -total- leaves it behind in e(b) [a matrix which you should then claim as your own with something like

      Code:
      matrix M = e(b)
      scalar points = M[1,1]
      It does not usually make sense to make a whole variable, with every observation repeating a single number.


      Code:
      egen points=total(X) if stu_id==710740 & hsflag==1
      will calculate this same number, and it will show up as such, but only in those observations where stu_id == 710740 and hsflag == 1. The other observations will show a missing value: the -if- qualifier determines not only which observations go into the calculation of points, but also which ones receive the result. If what you need to do is create a variable points that contains this single value of X in all observations, but is calculated only using those values of X where stu_id == 710740 & hsflag ==1, then your code would be:

      Code:
      egen points = total(X*(stu_id==710740 & hsflag ==1))
      But, again, I don't see the point in creating a variable all of whose observations repeat this same value.

      Hope this helps.

      Comment


      • #4
        Ok, thank you, all of this is very helpful. To clarify, my goal is to obtain a scalar that I can use in subsequent calculations.

        Clyde's last line of code is what I want, except I want to store the result in a scalar.

        Comment


        • #5
          Clyde also gave all the information you need, as a single total is the first (and only) value of e(b). See his second Code box.

          Comment

          Working...
          X