Announcement

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

  • Summing two rows across columns

    Hi. I have illness and mortality data for Indian states by date. For two of the Indian states - Ladakh, and Jammu and Kashmir - I want to sum the data on the same date across all variables into a single row, where the state name is J&K. For example, on 4/1/2020, the new row of J&K would have the following values confirmed =75, recovered=5, deceased=2.

    Any guidance on how to do so would be much appreciated.

    Code:
    
    clear
    input str10 date str40 state long(confirmed recovered deceased)
    "4/1/2020"  "Jammu and Kashmir"  62   2 2
    "4/1/2020"  "Ladakh"             13   3 0
    "4/2/2020"  "Jammu and Kashmir"  70   3 2
    "4/2/2020"  "Ladakh"             13   3 0
    "4/3/2020"  "Jammu and Kashmir"  75   3 2
    "4/3/2020"  "Ladakh"             14   3 0
    "4/4/2020"  "Jammu and Kashmir"  92   3 2
    "4/4/2020"  "Ladakh"             14   3 0
    "4/5/2020"  "Jammu and Kashmir" 106   4 2
    "4/5/2020"  "Ladakh"             14   3 0
    "4/6/2020"  "Jammu and Kashmir" 109   4 2
    "4/6/2020"  "Ladakh"             14  10 0
    "4/7/2020"  "Jammu and Kashmir" 125   4 3
    "4/7/2020"  "Ladakh"             14  10 0
    "4/8/2020"  "Jammu and Kashmir" 158   6 3
    "4/8/2020"  "Ladakh"             14  10 0
    "4/9/2020"  "Jammu and Kashmir" 184   6 4
    "4/9/2020"  "Ladakh"             14  10 0
    "4/10/2020" "Jammu and Kashmir" 207   6 4
    "4/10/2020" "Ladakh"             15  11 0
    "4/11/2020" "Jammu and Kashmir" 224   6 4
    "4/11/2020" "Ladakh"             15  11 0
    "4/12/2020" "Jammu and Kashmir" 245   6 4
    "4/12/2020" "Ladakh"             15  11 0
    Last edited by Scott Rick; 28 Jun 2023, 10:12.

  • #2
    Code:
    tempfile copy
    save `copy'
    keep if inlist(state, "Jammu and Kashmir", "Ladakh")
    collapse (sum) confirmed recovered deceased, by(date)
    gen state = "J & K"
    append using `copy'
    drop if inlist(state, "Jammu and Kashmir", "Ladakh")

    Comment


    • #3
      Thank you Clyde Schechter. That worked perfectly!

      Comment

      Working...
      X