Announcement

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

  • Bysort

    Hello,

    I have reproduced a simple replica of a bigger problem.

    I have data which looks as attached. I need, the value against HS 3201 as the sum of values against HS 32010, 32011, 32012, 32013 and similarly for HS 3202 to be the addition of values against 32021, 32022, 32023, 32024.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(HS Value Desired)
     3201  .  10
    32010  1  10
    32011  2  10
    32012  3  10
    32013  4  10
     3202  . 110
    32021 11 110
    32022 22 110
    32023 33 110
    32024 44 110
    end

    Request your help for the relevant bysort / any other command to perform this.

    Thanks in advance!

  • #2
    Thanks for the data example.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(HS Value Desired)
     3201  .  10
    32010  1  10
    32011  2  10
    32012  3  10
    32013  4  10
     3202  . 110
    32021 11 110
    32022 22 110
    32023 33 110
    32024 44 110
    end
    
    gen HS2 = cond(HS < 10000, HS, floor(HS/10)) 
    
    egen Wanted = total(Value), by(HS2) 
    
    list, sepby(HS2) 
    
         +-----------------------------------------+
         |    HS   Value   Desired    HS2   Wanted |
         |-----------------------------------------|
      1. |  3201       .        10   3201       10 |
      2. | 32010       1        10   3201       10 |
      3. | 32011       2        10   3201       10 |
      4. | 32012       3        10   3201       10 |
      5. | 32013       4        10   3201       10 |
         |-----------------------------------------|
      6. |  3202       .       110   3202      110 |
      7. | 32021      11       110   3202      110 |
      8. | 32022      22       110   3202      110 |
      9. | 32023      33       110   3202      110 |
     10. | 32024      44       110   3202      110 |
         +-----------------------------------------+

    Comment


    • #3
      Thank you very much Nick!

      Comment

      Working...
      X