Announcement

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

  • The use of slash in Stata

    While I know that the following command will sum ri where the variable pic is equal to USA and spread the sum to all observation whether or not pic is equal to USA. However, I am not sure what specifically the slash does that the if condition cannot. also, what other useful uses of slash are availabe in Stata.
    Code:
    egen GDPref = total( ri / ( pic == "USA"))
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    Attaullah omits the context of http://www.statalist.org/forums/foru...-a-local-macro

    Here, as usual outside filenames, slash just means divide: no more, no less.

    But in Stata dividing by 0 in particular means a missing result, sometimes bad news, but not necessarily in contexts where missings are ignored and that is precisely what you want.

    Here the total() function just ignores missings and in the particular example that ensures that the values for the USA are spread to other values in the same year, thus providing a reference level.

    More generally, the construct is useful mainly because of the way it works for true and false results for the logical comparison, in this case

    Code:
    pic == "USA"
    False as a result for that comparison followed through means 'divide by 0' and people's mathematical education usually imparts a strong sense of sin, impropriety or danger to that division. But the effect can be what you want.

    Studying the following example will show that what looks like the equivalent if condition has the side-effect that missings are generated whenever that condition is not true. But doing it as above means that non-missing values are returned routinely.

    The example below uses groups from SSC.

    Note that

    Code:
    . egen invest45 = total(cond(year == 1945, invest, .)), by(company)
    has precisely the same effect.

    Much more at http://www.stata-journal.com/sjpdf.h...iclenum=dm0055

    Code:
    . webuse grunfeld
    
    . su year
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
            year |        200      1944.5    5.780751       1935       1954
    
    . egen invest45 = total(invest / (year == 1945)), by(company)
    
    . egen invest45_2 = total(invest) if year == 1945 , by(company)
    (190 missing values generated)
    
    . groups company invest45
    
      +--------------------------------------+
      | company   invest45   Freq.   Percent |
      |--------------------------------------|
      |       1      561.2      20     10.00 |
      |       2      258.7      20     10.00 |
      |       3       93.6      20     10.00 |
      |       4      88.78      20     10.00 |
      |       5      63.21      20     10.00 |
      |--------------------------------------|
      |       6      39.03      20     10.00 |
      |       7      44.12      20     10.00 |
      |       8      39.27      20     10.00 |
      |       9      52.32      20     10.00 |
      |      10       1.36      20     10.00 |
      +--------------------------------------+
    
    
    . groups company invest45_2, missing sepby(company)
    
      +--------------------------------------+
      | company   invest~2   Freq.   Percent |
      |--------------------------------------|
      |       1      561.2       1      0.50 |
      |       1          .      19      9.50 |
      |--------------------------------------|
      |       2      258.7       1      0.50 |
      |       2          .      19      9.50 |
      |--------------------------------------|
      |       3       93.6       1      0.50 |
      |       3          .      19      9.50 |
      |--------------------------------------|
      |       4      88.78       1      0.50 |
      |       4          .      19      9.50 |
      |--------------------------------------|
      |       5      63.21       1      0.50 |
      |       5          .      19      9.50 |
      |--------------------------------------|
      |       6      39.03       1      0.50 |
      |       6          .      19      9.50 |
      |--------------------------------------|
      |       7      44.12       1      0.50 |
      |       7          .      19      9.50 |
      |--------------------------------------|
      |       8      39.27       1      0.50 |
      |       8          .      19      9.50 |
      |--------------------------------------|
      |       9      52.32       1      0.50 |
      |       9          .      19      9.50 |
      |--------------------------------------|
      |      10       1.36       1      0.50 |
      |      10          .      19      9.50 |
      +--------------------------------------+

    Comment


    • #3
      Thank you Nick, great explanation
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment

      Working...
      X