Announcement

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

  • Do sum of all corresponding values in one column when other column has a certain value

    I want to sum the column 'stat1' by the column 'Group' if it has a certain value, such as Group == 'AAA'


    Group sub-group code Name stat1 stat2 stat3
    AAA AA wsed A 2 5 20
    gfb B 54 43 22
    BB wewe C 33 43 35
    qsq D 4 20 43
    BBB CC nhn K 54 43 22
    asa L 4 8 8
    DD dsd A 67 8 2
    ss B 56 7 45


    In non stata-syntax, I m looking for some command on the following lines:

    Do sum of all corresponding values in column stat1 when value of Group == "AAA"

    I would appreciate if some Stata user can help
    Last edited by Moaiz Siddiqui; 14 Nov 2019, 03:23.

  • Nick Cox
    replied
    Here is one to do it using groups (Stata Journal):

    Code:
    . sysuse auto, clear 
    (1978 Automobile Data)
    
    . egen wanted = total(price), by(rep78) 
    
    . groups wanted rep78, show(none) order(high) colorder(2) 
    
      +----------------+
      | rep78   wanted |
      |----------------|
      |     3   192877 |
      |     4   109287 |
      |     5    65043 |
      |     2    47741 |
      |     1     9129 |
      +----------------+

    Leave a comment:


  • Moaiz Siddiqui
    replied

    Thank you Mr. Nick Cox. This was very helpful.
    I have to ask just one more small thing:

    The code:
    tabdisp rep78, c(wanted)


    tabulates the variable 'rep78' in alphabetical order and the corresponding numerical values of the variable 'wanted' in the next column.

    In case, I want to display 'rep78' in terms of numerical values of 'wanted' in descending order, how will the code change?
    Will appreciate your help in this matter.

    Regards,

    Moaiz

    Leave a comment:


  • Nick Cox
    replied
    Consider the following silly example, which you can run. Here rep78 has the same role as Group in your example as I understand it.


    Code:
    sysuse auto, clear 
    egen wanted = total(price), by(rep78) 
    tabdisp rep78, c(wanted)
    Also check out

    Code:
    help tabstat
    However, note that Stata will not understand empty strings as meaning use the non-empty value above.

    Leave a comment:

Working...
X