下载多个Excel表格到Stata,每个表格代表特定年份的数据集。

huangapple go评论128阅读模式
英文:

how to download into stata several excel sheet each one represents a data set for specific year

问题

  1. // 设置工作目录为包含Excel文件的文件夹
  2. cd "C:\Users\noura\OneDrive\Desktop\project excel";
  3. // 循环遍历每一年,将相应的Excel文件导入Stata
  4. foreach file of varlist 2013/2022 {
  5. import excel "`file'.xlsx", firstrow clear
  6. gen year = "`file'"
  7. append using "`file'.xlsx"
  8. }
  9. // 按年份对数据进行排序
  10. sort year
英文:

am new to stata, this is my first time to use it 下载多个Excel表格到Stata,每个表格代表特定年份的数据集。
i have the data set for firms operate in kuwait market for 10 years, from 2013 to 2022, each year in a different excel files. so i have total 10 excel files, which i saved them in one folder, the folder bath "C:\Users\noura\OneDrive\Desktop\project excel"
excel files name:
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022

so how i can import the 10 excel files into Stata and make sure that Stata are reading the data as continues years from 2013 to 2022.

what is the codes i should use?

i tried several code but nothing work

  1. // Set the working directory to the folder where your Excel files are saved
  2. cd "C:\Users\noura\OneDrive\Desktop\project excel"
  3. // Loop over each year and import the corresponding Excel file into Stata
  4. foreach file of varlist 2013/2022 {
  5. import excel "`file'.xlsx", firstrow clear
  6. gen year = "`file'"
  7. append using "`file'.xlsx"
  8. }
  9. // Sort the data by year
  10. sort year

答案1

得分: 1

您离解决方案非常接近。首次使用Stata时做得很好。

您的代码问题在于清除命令 clear

  1. import excel "file.xlsx", firstrow clear

它清除了内存中的所有数据。因此,在每个循环中,您都在删除以前的所有年份数据。在我修改您示例代码中,我设置了一个 tempfile 用于在每个循环结束时保存数据,然后将年份数据追加到该数据集。

  1. // 将工作目录设置为保存Excel文件的文件夹
  2. cd "C:\Users\noura\OneDrive\Desktop\project excel"
  3. clear
  4. tempfile allyears
  5. save `allyears', emptyok
  6. // 循环处理每一年并将相应的Excel文件导入Stata
  7. foreach file of varlist 2013/2022 {
  8. import excel "`file'.xlsx", firstrow clear
  9. gen year = "`file'"
  10. append using `allyears'
  11. save `allyears', replace
  12. }
  13. // 按年份对数据进行排序
  14. sort year

希望这能帮助您解决问题。

英文:

You were very close to a solution. Well done for being your first time using Stata.

The problem in your code is that clear in

  1. import excel "`file'.xlsx", firstrow clear

clears all the data currently in memory. So in each loop you are deleting all previous years. In my modification of your example code I have set up a tempfile that you use to save the data to at the end of each loop. And then you append your year data to that data set.

  1. // Set the working directory to the folder where your Excel files are saved
  2. cd "C:\Users\noura\OneDrive\Desktop\project excel"
  3. clear
  4. tempfile allyears
  5. save `allyears', emptyok
  6. // Loop over each year and import the corresponding Excel file into Stata
  7. foreach file of varlist 2013/2022 {
  8. import excel "`file'.xlsx", firstrow clear
  9. gen year = "`file'"
  10. append using `allyears'
  11. save `allyears', replace
  12. }
  13. // Sort the data by year
  14. sort year

huangapple
  • 本文由 发表于 2023年4月11日 07:15:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定