跳轉到內容

Stata/程式設計

來自華夏公益教科書,開放的書籍,面向開放的世界

Stata 包含兩種型別的宏:區域性和全域性。

  • Stata 中的程式是一種函式。它接受引數併產生結果。
  • 它檢查程式是否已經存在。
. which hello
/Applications/Stata/ado/personal/hello.ado
which hello
cap program drop hello  
program define hello
	di "say hello"
end
hello
program drop hello

程式也可以返回一個 r 類物件。

. clear
. set obs 100
obs was 0, now 100
. gen u=invnorm(uniform())
. *** rclass
. program example, rclass
  1.  return scalar x=1
  2. end
. example
. ret list
scalars:
                 r(x) =  1

傳遞引數到程式

[編輯 | 編輯原始碼]

預設情況下,引數使用宏 `1`、`2`、…、`N` 呼叫。以下是如何工作的示例。該程式非常簡單,只是顯示任何引數。

. cap program drop tester

. program tester
  1.  di "argument 1 is |`1'|"
  2.  di "argument 2 is |`2'|"
  3.  di "argument 3 is |`3'|"
  4.  di "argument 4 is |`4'|"
  5. end

. 
. tester
argument 1 is ||
argument 2 is ||
argument 3 is ||
argument 4 is ||

. tester a b c
argument 1 is |a|
argument 2 is |b|
argument 3 is |c|
argument 4 is ||

. tester ab cd ee pei 
argument 1 is |ab|
argument 2 is |cd|
argument 3 is |ee|
argument 4 is |pei|

個人 ado 檔案

[編輯 | 編輯原始碼]

最佳實踐是將您的個人 ado 檔案儲存在個人目錄中。

. personal dir
your personal ado-directory is /Applications/Stata/ado/personal/

幫助檔案

[編輯 | 編輯原始碼]

幫助檔案使用特定的標記語言。您可以在文字編輯器中開啟幫助檔案並檢視語法。

類 Unix 函式

[編輯 | 編輯原始碼]
  • rmdir 刪除目錄
  • mkdir 建立一個新目錄
  • erase 刪除檔案
erase temp.dta
  • shell! 執行 unix/dos 命令。例如,在 unix 環境(Mac/Linux)中,可以使用 iconv 命令列將編碼從 macroman 更改為 latin1。
! iconv -f latin1 -t macroman dataxmlWin.xml > dataxmlMac.xml
  • type 在結果視窗中列印檔案。
type textfile.txt
  • copy 將檔案複製到磁碟
copy https://wikibook.tw/wiki/Stata wikibook.txt
  • filefilter 在文字檔案中用另一個字元替換一個字元。以下命令從 temp1.txt 中刪除引號並用 "-" 替換它們。
filefilter temp1.txt temp2.txt, from("\Q") to("-") replace
  • tmpdir 提供臨時工作目錄

編碼問題

[編輯 | 編輯原始碼]

有時,您需要將資料集的編碼從 latin1 轉換為 macroman。您可以使用 unix iconv 命令。

cap prog drop win2mac
prog define win2mac
	args basewin 
	use `basewin', clear
	xmlsave dataxmlWin, doctype(dta) replace
	! iconv -f latin1 -t macroman dataxmlWin.xml > dataxmlMac.xml
	xmluse dataxmlMac.xml, doctype(dta)
end

Shell 程式呼叫 Stata

[編輯 | 編輯原始碼]

對於持續的專案,您可能希望將 Stata 指令碼嵌入到自動化工作流中。本節將向您展示如何從命令列執行 Stata do 檔案。對於這些示例,您必須將 Stata 可執行檔案的資料夾包含到您的 PATH 環境變數中。根據您的 Stata 版本(SE、MP、IC、Small),Stata 可執行檔案可能也具有略微不同的名稱。

如果您只是用 do 檔案作為其唯一引數來呼叫 Stata,例如

StataSE myscript.do

那麼 Stata 將以正常(互動式)模式開啟,並根據您的使用者偏好執行以下操作之一。它可能會執行指令碼(執行 -do myscript.do-)或開啟 do 檔案編輯器(-doedit myscript.do-)。要設定此偏好,請開啟 do 檔案編輯器,然後轉到“編輯”、“偏好”,並設定“從 Windows 開啟 do 檔案而不是執行它們”的選項。

或者,您可以以後臺(批處理)模式呼叫 Stata,例如

StataSE /e do myscript.do

這將執行您的指令碼,使 Stata 視窗保持最小化,並自動將所有內容記錄到當前工作目錄中的 myscript.log(在批處理模式下無法關閉此自動記錄)。

在批處理模式下呼叫 shell 命令(!/shell/winexec)將無法從 Windows[1] 中工作。依賴這些命令的程式在這種情況下也將無法工作。示例包括:dirlist[2] 和 parallel。

預設情況下,Stata 將從 Shell 繼承工作目錄。例如

cd /myproject/
Stata /e do code/myscript.do

Stata 將以 /myproject 作為工作目錄執行。日誌檔案也將位於那裡,因此這是一種將日誌檔案儲存在與 do 檔案不同的目錄中的方法。

參考文獻

[編輯 | 編輯原始碼]
  1. http://www.stata.com/statalist/archive/2010-01/msg00537.html
  2. http://www.stata.com/statalist/archive/2011-07/msg00559.html
上一個:圖形 索引 下一個:Mata
華夏公益教科書