Announcement

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

  • How can I add up the data in one variable which are restricted by another one variable?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str2(SelectedNomen NativeNomen) int Reporter str5 ReporterName long HSCode byte Partner str6 PartnerName int(TariffYear TradeYear) str3(TradeSource DutyType) double SimpleAverage
    "HS" "H1" 156 "China"   101 0 " World" 1998 1998 "CMT" "MFN"    5
    "HS" "H1" 156 "China"   102 0 " World" 1998 1998 "CMT" "MFN"    5
    "HS" "H1" 156 "China"   103 0 " World" 1998 1998 "CMT" "MFN" 6.67
    "HS" "H1" 156 "China"   104 0 " World" 1998 1998 "CMT" "MFN"    5
    end
    For a continuous HS four-digit code tariff data set(as shown in the above section), how to use stata to sum up the number of tariffs corresponding to HS four-digit code 8417 8421-8422 8424-8449 8451?

    Thank you in advance. I am happy to provide more information.

    Zhangningning

  • #2
    Code:
    egen wanted= total(cond(SelectedNomen=="HS" &(inlist(HSCode, 8417, 8421, 8422, 8451)|inrange(HSCode, 8424, 8449)), 1, .))
    For a discussion of this technique, see https://journals.sagepub.com/doi/pdf...867X1101100210.

    But note that the condition evaluates to true or false, so you need not use the -cond()- function:

    Code:
    egen wanted= total(SelectedNomen=="HS" &(inlist(HSCode, 8417, 8421, 8422, 8451)|inrange(HSCode, 8424, 8449)))
    Last edited by Andrew Musau; 20 Feb 2023, 00:57.

    Comment


    • #3
      OK,I appreciate it. Thank you very much!

      Comment

      Working...
      X