




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、第六講第六講 stata程序編寫與管理程序編寫與管理第一種方法:直接寫第一種方法:直接寫do file 打開do編輯器: doedit 一個簡單的do file display “hello,world” exit /告訴stata在這程序結(jié)束,exit可不寫 保存為 hello.do 在command 窗口輸入 do hello stata 會顯示 display “hello,world” hello,world第二種:在第二種:在stata窗口中輸入窗口中輸入 stata顯示: program hello 1. display ”hello,world“ 2. end 執(zhí)行: hello
2、 顯示: hello,world 將hello,world修改為hello,cufe program hello hello already defined r(110) 解決方法:program drop hello注意,program名不能與stata中的命令名一致 program des display ”hello,world“ end第二種:在第二種:在stata窗口中輸入窗口中輸入 program hello display hello,cufe end 查找語法錯誤: set trace on 關(guān)閉該功能: set trace off第三種:第三種:do file中的中的prog
3、ram program hello display “hello,world” end stata中輸入: do hello stata 顯示: hello already defined r(110) stata 輸入: program drop hello do hello /或用 run hello hello第四種:第四種:do file的擴展的擴展 program hello dis “hello,world” end hello exit 如果加上program drop hello 解決方法: capture 第五種:第五種:ado file ado file是stata中的可執(zhí)
4、行文件 program hello dis “hello,world” end exit 執(zhí)行時輸入: program drop hello hello stata顯示: hello,world ado file的保存地址的保存地址 ado file只有放在指定的文件夾中才能運行 adopath命令 adopath + c:adopersonal /增加新的adofile存放地址 adopath - c:adopersonal /移除ado file目錄 注意: 可以將自己的程序統(tǒng)一存放于 D:stataadopersonalmyado 同時在pro文件中做如下定義 adopath + D:s
5、tataadopersonalmyado 該文件夾下可以進一步設(shè)定a-z的子文件夾 一個完整的一個完整的do file文件文件 capture log close /檢查log的狀態(tài)為close log using x,replace /打開log x set more off capture program drop hello program hello dis “hello,world” end log close / 關(guān)閉log exit /保存為sj.do do file的引用的引用 do sj exit /保存為sj2.do do sj2 assert 的用法的用法 assert是
6、stata的重要命令,如果assert后的表達式為true,則stata繼續(xù)執(zhí)行命令,否則stata會提示出錯 capture program drop sj2 sysuse auto,clear assert foreign2 exit /保存為sj2.dopreserve的用法的用法 preserve 可以避免數(shù)據(jù)在程序執(zhí)行后有所變動 sysuse auto,clear preserve / 備份當(dāng)前狀態(tài)S1 drop if price10000 sum save nauto,replace restore /恢復(fù)到狀態(tài)S1 sum use nauto,clear quietly的用法的用
7、法 quietly可以避免列印過多的結(jié)果 比較兩段代碼 capture program drop sj program sj sysuse auto,clear drop if price1000 save nauto,replace end capture program drop sj program sj sysuse auto,clear quietly drop if price1000 save nauto,replace end 單值單值 Scalar 存放數(shù)值 scalar a=3 scalar b=ln(a)+5 dis a dis b 存放字符串 scalar c=.a d
8、is c scalar s1=“hello,world” scalar s2=substr(s1,1,5) dis s1 dis s2 執(zhí)行命令后的單值結(jié)果執(zhí)行命令后的單值結(jié)果 sysuse auto,clear sum price return list dis r(N) scalar range=r(max)-r(min) dis range gen qq=r(sd) list qq in 1/10 單值單值 管理管理 scalar dir scalar list /顯示單值的內(nèi)容 scalar drop a / 刪除單值 scalar list scalar drop all /刪除所有
9、單值 scalar list 暫元變量暫元變量local macro local:在一個do或ado file中發(fā)揮作用暫元的定義與引用 存放數(shù)字 local a=5 dis a local b=a+7 dis b 存放文字 local name1 “sj:” dis “name1” local name2 “中央財經(jīng)大學(xué)會計學(xué)院” dis “name2” local name3 name1name2 dis “name3” 存放變量名稱 sysuse auto,clear local varllist price weight sum varlist Local:數(shù)學(xué)運算符的處理:數(shù)學(xué)運算符
10、的處理 local a “2+2” dis a dis “a” / 看這兩個有何區(qū)別 local b=2+2 / 注意與a的定義的區(qū)別 dis b dis “b” 暫元中的暫元暫元中的暫元 local a1=2 local a2 “var” local a3=2*a1 local a4 aa1 local a2a1=2*a3 dis a1 dis “a2” dis a3 dis “a4”全局暫元全局暫元global macro:定義與引用方式:定義與引用方式 全局暫元:在整個stata運行的過程中一直存在 定義與引用方式: global aa “This is my first program
11、!” dis “$aa” global x1=5 global x2=2$x1 dis $x2 示例: sysues auto,clear global option “,robust” global reg “regress” local x1 “price mpg foreign” $reg rep78 x1 $option local x2 “price mpg foreign trunk” $reg rep78 x1 $option $reg rep78 x2 $option 暫元的管理暫元的管理 macro list macro dir macro drop x2 macro dir
12、 x2 macro dir aa 暫時性變量暫時性變量 tempvar sysuse auto ,clear tempvar x1 x2 gen x1=price*2 gen x2=ln(rep78) sum x1 x2 暫時性變量可以與永久變量同名循環(huán)語句循環(huán)語句 while 語句 forvalues 語句 foreach 語句條件循環(huán)語句:條件循環(huán)語句:while local j=0 while j5 dis aj local j=j+1 循環(huán)語句:循環(huán)語句:forvalues local i=1 local j=_N forvalues i=1(1)j dis aI forvalues
13、i=0(-1)-14 dis ai forvalues應(yīng)用示例應(yīng)用示例假設(shè)你有100個文件,分別為d1.dta,d2.dta,d100.dta 研究要求將這100個文件做縱向合并,寫出程序 use d1.dta,clear local i=2 forvalues i=2(1)100 append using di.dta save finaldata,replace循環(huán)語句:循環(huán)語句:foreach foreach var of varlist x y z command 示例1:將auto.dta各變量的對數(shù)轉(zhuǎn)換和縮尾處理 sysuse auto,clear local vars price
14、 weight length foreach v of varlist vars gen lnv=ln(v) winsor v,gen(vw) p(0.01) 條件語句:條件語句:if語法格式 格式1 if (條件) command 格式2 if(條件1) command1 else if(條件2) /這里可以沒有if command2 條件語句:條件語句:if示例示例 scalar aa=1 if aa=1 dis “這小子真帥!” else if aa=0 dis “這女孩真亮!” 引用引用stata命令的返回值命令的返回值 留存在內(nèi)存中的結(jié)果 r-class ,與模型估計無關(guān)的命令,如sum e-class, 與模型估計有關(guān)的命令,如regress s-class,其他命令, 如list c-class,存儲系統(tǒng)參數(shù) 顯示留存值的
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 護理風(fēng)險評估體系構(gòu)建與應(yīng)用
- 護士長的科室管理
- 電氣設(shè)備認識實訓(xùn)的心得體會模版
- 固定資產(chǎn)臺賬管理報告
- 小米手機發(fā)布會課件
- 大學(xué)生職業(yè)規(guī)劃大賽《財政學(xué)專業(yè)》生涯發(fā)展展示
- 崩漏的護理查房
- 郵政銀行筆試題目及答案
- 支教活動總結(jié)模版
- 一級消防員考試題及答案
- 財產(chǎn)申報表-被執(zhí)行人用
- 委托聘請演員合同協(xié)議
- 水庫防汛知識培訓(xùn)
- 2025年貴州省遵義市中考一模英語試題(含筆試答案無聽力原文及音頻)
- 安徽省C20教育聯(lián)盟2025年九年級中考“功夫”卷(二)數(shù)學(xué)
- 《影視廣告創(chuàng)意與制作》課件 教學(xué)項目6 蒙太奇剪輯
- 2025年徐州市專業(yè)技術(shù)人員公需課程 - 心理調(diào)適
- 《中國餐桌禮儀》課件
- 自愿援疆申請書
- 智慧照明系統(tǒng)施工方案
- DB37-T 5061-2024 住宅小區(qū)供配電設(shè)施建設(shè)標(biāo)準(zhǔn)
評論
0/150
提交評論