Announcement

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

  • Problems corresponding to variable names in stata double-layer loops

    I encountered a problem in the process of data merging. I put 5 control variables into 5 sheets of an excel table. After converting the panel data, I want to put the corresponding variable name in each sheet.After changing it, although the variable name has been changed, the dta name has also been changed, and the dta data generated all have become the data in the last sheet. Please help me and answer it, thank you very much! ! ! ! (below is the stata code I wrote)
    local x cpi house profits population rgdp
    forvalues i = 1/5{
    foreach m of local x{
    import excel using control.xlsx,sheet(Sheet`i') first clear
    reshape long M, i(province) j(year)
    rename M `m'
    save `m'.dta,replace
    }
    }

  • #2
    I guess you need loops in parallel, not nested loops. Consider


    Code:
    local x cpi house profits population rgdp
    forvalues i = 1/5 {
          import excel using control.xlsx,sheet(Sheet`i') first clear
          reshape long M, i(province) j(year)
          gettoken m x : x
          rename M `m'
          save `m'.dta,replace
    }
    For much more discussion, see https://journals.sagepub.com/doi/pdf...6867X211063415
    Last edited by Nick Cox; 01 Jun 2022, 02:47.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      I guess you need loops in parallel, not nested loops. Consider


      Code:
      local x cpi house profits population rgdp
      forvalues i = 1/5 {
      import excel using control.xlsx,sheet(Sheet`i') first clear
      reshape long M, i(province) j(year)
      gettoken m x : x
      rename M `m'
      save `m'.dta,replace
      }
      For much more discussion, see https://journals.sagepub.com/doi/pdf...6867X211063415
      The problem is solved. Thanks very much!!!

      Comment

      Working...
      X