加载中…
个人资料
flemynglee
flemynglee
  • 博客等级:
  • 博客积分:0
  • 博客访问:3,698
  • 关注人气:53
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Stata Tips: 从Stata读入Excel数据

(2013-11-07 23:37:53)
分类: 经济学家们

How can I read Excel files in Stata? (Stata 12)

In the past we have recommended reading Excel files by first saving them as comma separated (.csv) files and then using insheet. Beginning in Stata 12 you can read Excel (.xls and .xlsx) files directly using the import excel command. To illustrate how this is accomplished we have an Excel file named hospital.xls.

The file, hospital.xls, has four Sheets each with the format. In cell A! is the hospital name. In cells B2:D5 are the data for each hospital. The variables names are listed in Row 2. Here is a sample of what one of the Sheets looks like:

 

A

B

C

D

1

santa monica

 

 

 

2

 

x

y

z

3

 

39

68

8

4

 

19

8

3

5

 

5

82

6

As you can see, the hospital is Santa Monica and the variable names are xy and z. Now here is what we want to do with the Sheets in this Excel file. We want to capture the hospital name and add to each row of the data. Then we want to append the data from each of the hospitals together into a single Stata data file. Additionally, we want to create a numeric variable that codes the hospital name. Here is the code to do this which you can paste into your do-file editor.

 

 

 

forvalues i=4(-1)1 {

 

 

  import excel using hospital.xls, ///

         sheet(Sheet`i') cellrange(A1:A1) clear

  local hname = A[1]   

      

       

  import excel using hospital.xls, ///

         sheet(Sheet`i') cellrange(B2) firstrow clear

  generate str12 hname = "`hname'"  

 

 

  if `i'==4 {

    save hospital, replace

  }

  else {

    append using hospital.dta

    save hospital, replace

  }

 

}

 

encode hname, gen(hnum)   

order hname hnum          

save hospital, replace<   

The two import excel commands are worth commenting on in detail. The first one looks like this:

import excel using hospital.xls, ///

         sheet(Sheet`i') cellrange(A1:A1) clear

We use the full file name with the .xls extension following using. The option sheet, of course, indicates which Sheet (1 thru 4) that we want to read. The cellrangeoption, in this case, indicates that we want to read a single cell (cell A1). The clear option clears out any data already in memory.

Here is the second import excel command:

import excel using hospital.xls, ///

         sheet(Sheet`i') cellrange(B2) firstrow clear

The sheet and clear options are the same as before. This time the cellrange option has just a single values, B2, which indicates the upper left-hand starting location to begin reading the data. We have added the firstrow option to indicate that the first row contains the variable names. When you run the do-file you end up with a dataset that looks like this:

. list, clean nolabel

 

              hname   hnum       

  1.           ucla       37   70   

  2.           ucla       15   80   

  3.           ucla       29   85   

  4.   santa monica       39   68   

  5.   santa monica       19   82   

  6.   santa monica       25   82   

  7.         harbor       38   67   

  8.         harbor       20   80   

  9.         harbor       25   82   

 10.         county        40   69   10 

 11.         county       20   80   

 12.         county       23   79   

And that's how to read Excel files in Stata 12.

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.

 链接:http://www.ats.ucla.edu/stat/stata/faq/read_excel.htm

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有