Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 73275

Local macro to import multiple Excel files

$
0
0
I am trying to compile data from a series of Excel workbooks and worksheets, while I have something that works it is rather inefficient coding. I have got company data from 30 companies in Excel format, and these Excel workbooks are in (relatively) consistent structure. The approach I've taken is to simply list each company every time I need to use that foreach loop. For example:
:
 
foreach company in "Company Alpha" "Company Beta" "Company Gamma"  {
  import excel "`company'", sheet("sheetname") clear
  *code applicable to each company
  }
However rather than listing all 30 company names every time, I thought I could apply a local macro, but regardless of how I structure the local macro I cannot seem to make it work.
:
local names `" "Company Alpha" "Company Beta" "Company Gamma" "'
foreach company in `names' {
   import excel "`company'", sheet("sheetname") clear
  *code applicable to each company
  }
This does nothing. I tried adding extra " " in the loop, but get an r(100) using error. Any advice on what I am doing wrong, or if it can be done. Any assistance would be much appreciated

PS - I did consider using a local files macro, and had it working to an extent, but because I also use the company loop names to define variables and save data file makes this method not ideal as it loops the full file name rather than the company name
:
local files : dir "C:\Users\MichaelW\Desktop\Stata working drive" files "*.xlsx"
foreach company in `files' {
 *code applicable to each company
  generate company="`company'"
  generate year="`year'"  
  save "C:\Users\MichaelW\Desktop\Stata working drive" files\_`company'.dta", replace
}

Viewing all articles
Browse latest Browse all 73275

Trending Articles


FLASHBACK WITH SIRASA FM AT GALGAMUWA 2022


Mp3 Download: Mdu - Mazola


Imitation gun was fired at motorist in Leicester road-rage incident


Ndebele names


MCKINNEY EMALINE “EMMA” OF WES...


Okra & Motia — The Workshop (Prod by Hammer)


Skint TV teen to be sentenced


Moondru Mudichu 19-09-2017 – Polimer tv Serial


YOSVANI JAMES Arrested by Miami-Dade County Corrections on Jan 10, 2017


Stories • Goddess Stepmom



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>