2015-06-28 36 views
0

我剛開始使用Stata,我無法弄清楚以下內容。如何遍歷Excel表格和Stata中的列表?

  1. 如何遍歷Excel工作表和索引列表。這現在工作正常。

    clear all 
    set more off 
    
    local mysheets 1996 2000 2003 2007 2008 2010 
    local indices index1 index2 index3 
    
    foreach sheetname of local mysheets { 
    
        import excel "C:\stata\Data.xls", sheet(`sheetname') firstrow clear 
    
        foreach index of local indices{ 
         tobit theta index, ll(0) ul(1) 
         outreg using "C:\stata\results.doc" , `append' 
         local append "append"  
        } 
    
    } 
    
+1

請注意,你的'local's被命名爲不同的:'mysheets'與'MySheet的工作' –

+0

另外,你在定義它之前使用'append''(注意''''的錯誤使用;我無法弄清楚如何讓SO在這種情況下使用適當的反作用力)。目前還不清楚爲什麼你會首先定義它(至少在這方面)。 –

+0

@Brendan謝謝。修正了這一點,它仍然無法正常工作至於追加,我發現那樣如果文件沒有被創建,它不會第一次追加,而是剩下的追加。 – Ever

回答

3

剛剛張貼作爲一個答案(所以這個問題不會出現未回答的),因爲它似乎是一個簡單的編碼錯誤:

  • 確保當地的宏名是一致的整個(mysheetmysheets)爲foreach參數
  • 使用本地宏語法(在這種情況下,sheetnameforeach環內
  • 如果使用當地的宏來定義的outregappend選項,定義它的選項調用之前

    clear all 
    set more off 
    local mysheets 1996 2000 2003 2007 2008 2010 
    local indices index1 index2 index3 
    
    foreach sheetname of local mysheets { 
        import excel "C:\stata\Data.xls", sheet(`sheetname') firstrow clear 
        foreach index of local indices { 
         tobit theta `index', ll(0) ul(1) 
         local append "append"  
         outreg using "C:\stata\results.doc" , `append' 
        } 
    } 
    
+2

我可能還會添加Brendan以在嵌套的foreach循環內的索引宏變量中添加刻度:'tobit theta'''index',ll(0)ul(1)' – Parfait

+0

Thanks;只是糾正以前確定的項目,但你當然是正確的。 –