Hello.
I'm using Stata 15 and am working on compiling certain ranges from multiple Excel sheets into one Stata file (I can't provide a copy since it is proprietary data). My Excel file consists of over 20 sheets, each of which represent a construct such as "Dissatisfaction." Within the "Dissatisfaction" sheet, I have six dimensions of the construct for over 20 brands measured daily. I would like to write a loop that allows me to establish a longitudinal Stata data file with the following variables:
Date, Brand, Construct1-Dimension1, Construct1-Dimension2, Construct1-Dimension3,.......Construct3-Dimension6.
Here are some steps I took: first, I created variable Date and variable Brand. The following code does just that:
Next, I'm saving each construct (or part of the data from each sheet) in a separate .dta file and rename each variable to the following format: ConstructxDimensionz (Note: I have to rename the variables z1-z6 since the variables are identical on each sheet; in excel terms: the column names are the same on each sheet)
Next, I merge the four files:
As you can see, I'm pulling from three different sheets per excel file and three different cellranges. Since I have six annual files and need to do this for about 45 different brands, I would like to see if the above steps can be integrated into one loop. Considering the complexity of the data structure in Excel, I think one loop for each brand would be suffice since I can then append each combined file into one final data set. In other words, after putting all six annual files with data for Brand1 into one folder, I'm hoping to loop over the above commands to end up with a longitudinal data file for all six years, three constructs and one brand.
Any suggestions would be greatly appreciated.
Thank you!
I'm using Stata 15 and am working on compiling certain ranges from multiple Excel sheets into one Stata file (I can't provide a copy since it is proprietary data). My Excel file consists of over 20 sheets, each of which represent a construct such as "Dissatisfaction." Within the "Dissatisfaction" sheet, I have six dimensions of the construct for over 20 brands measured daily. I would like to write a loop that allows me to establish a longitudinal Stata data file with the following variables:
Date, Brand, Construct1-Dimension1, Construct1-Dimension2, Construct1-Dimension3,.......Construct3-Dimension6.
Here are some steps I took: first, I created variable Date and variable Brand. The following code does just that:
Code:
//import date and brand //pull brand name import excel "\\Client\C$\Users\..2007.xlsx", sheet("Dissatisfaction") cellrange(ED6:ED6) clear //save brand name in a local local brand=ED[1] //pull date import excel "\\Client\C$\Users\...2007.xlsx", sheet("Dissatisfaction") cellrange(A7:A372) firstrow clear //add brand name variable generate str12 brand = "`brand'" //save brand/year save "\\Client\C$\Users\....Brand1_2007.dta", replace
Code:
local sheets Dissatisfaction Construct2 Construct3 foreach x of local sheets { import excel "\\Client\C$\Users\..._2007.xlsx", sheet("`x'") cellrange(ED7:EI372) firstrow clear foreach var of varlist z1 z2 ... { rename `var' `x'`var' } save "\\Client\C$\Users\...\Brand1_`x'", replace }
Next, I merge the four files:
Code:
use "\\Client\C$\Users\...\Brand1_Dissatisfaction.dta" merge using "\\Client\C$\Users\...\Brand1_Construct2.dta" drop _merge merge using "\\Client\C$\Users\...\Brand1_Construct3.dta" drop _merge merge using "\\Client\C$\Users\...\Brand1_2007.dta" save "\\Client\C$\Users\...\Brand1_2007_combined.dta"
Any suggestions would be greatly appreciated.
Thank you!
Comment