Announcement

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

  • Export to Excel with multiple sheets

    My data set has one variable "id" with 98 observations. I want to split these ids into 9 parts and export them into an Excel file. To be specific, the Excel file will contain 9 sheets; sheets 1-8 will have 11 ids each, and sheet 9 will have the rest i.e. 10 ids. In addition, if Stata allows, I would like to rename sheet as List e.g. List1, List2 etc.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 id
    "130042"
    "130050"
    "130053"
    "130057"
    "130062"
    "130086"
    "130088"
    "130090"
    "130091"
    "130092"
    "130104"
    "130113"
    "130115"
    "130118"
    "130127"
    "130128"
    "13012F"
    "130131"
    "130132"
    "130134"
    "130137"
    "130143"
    "130144"
    "130146"
    "130147"
    "130148"
    "130149"
    "130156"
    "130158"
    "130159"
    "130160"
    "130161"
    "130165"
    "130166"
    "130169"
    "13016K"
    "13016L"
    "130170"
    "130171"
    "130174"
    "130184"
    "130185"
    "130190"
    "130191"
    "130192"
    "130193"
    "130194"
    "130195"
    "130202"
    "130203"
    "130204"
    "130209"
    "130211"
    "13021N"
    "130221"
    "130228"
    "130229"
    "130232"
    "130233"
    "130257"
    "130260"
    "130266"
    "130267"
    "130268"
    "130269"
    "130276"
    "130286"
    "130298"
    "130303"
    "130316"
    "130324"
    "130330"
    "130362"
    "130364"
    "130371"
    "130375"
    "130397"
    "13039P"
    "130402"
    "130409"
    "130411"
    "130422"
    "130485"
    "130488"
    "130495"
    "130496"
    "130499"
    "130502"
    "130511"
    "130514"
    "13052F"
    "13052H"
    "13052J"
    "13052N"
    "13052P"
    "130534"
    "130536"
    "130539"
    end

  • #2
    Code:
    gen int list_num = ceil(_n/11)
    
    forvalues i = 1/9 {
        export excel id using my_9_lists.xlsx if list_num == `i', ///
            sheet("List`i'") sheetreplace
    }

    Comment


    • #3
      Awesome! Thank you so much Clyde Schechter

      Comment

      Working...
      X