




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、第六講第六講 stata程序編寫與管理程序編寫與管理第一種方法:直接寫第一種方法:直接寫do file 打開do編輯器: doedit 一個(gè)簡(jiǎn)單的do file display “hello,world” exit /告訴stata在這程序結(jié)束,exit可不寫 保存為 hello.do 在command 窗口輸入 do hello stata 會(huì)顯示 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 查找語(yǔ)法錯(cuò)誤: 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的擴(kuò)展的擴(kuò)展 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í)行時(shí)輸入: program drop hello hello stata顯示: hello,world ado file的保存地址的保存地址 ado file只有放在指定的文件夾中才能運(yùn)行 adopath命令 adopath + c:adopersonal /增加新的adofile存放地址 adopath - c:adopersonal /移除ado file目錄 注意: 可以將自己的程序統(tǒng)一存放于 D:stataadopersonalmyado 同時(shí)在pro文件中做如下定義 adopath + D:s
5、tataadopersonalmyado 該文件夾下可以進(jìn)一步設(shè)定a-z的子文件夾 一個(gè)完整的一個(gè)完整的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后的表達(dá)式為true,則stata繼續(xù)執(zhí)行命令,否則stata會(huì)提示出錯(cuò) capture program drop sj2 sysuse auto,clear assert foreign2 exit /保存為sj2.dopreserve的用法的用法 preserve 可以避免數(shù)據(jù)在程序執(zhí)行后有所變動(dòng) 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:在一個(gè)do或ado file中發(fā)揮作用暫元的定義與引用 存放數(shù)字 local a=5 dis a local b=a+7 dis b 存放文字 local name1 “sj:” dis “name1” local name2 “中央財(cái)經(jīng)大學(xué)會(huì)計(jì)學(xué)院” dis “name2” local name3 name1name2 dis “name3” 存放變量名稱 sysuse auto,clear local varllist price weight sum varlist Local:數(shù)學(xué)運(yùn)算符的處理:數(shù)學(xué)運(yùn)算符
10、的處理 local a “2+2” dis a dis “a” / 看這兩個(gè)有何區(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:定義與引用方式:定義與引用方式 全局暫元:在整個(gè)stata運(yùn)行的過程中一直存在 定義與引用方式: 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 暫時(shí)性變量暫時(shí)性變量 tempvar sysuse auto ,clear tempvar x1 x2 gen x1=price*2 gen x2=ln(rep78) sum x1 x2 暫時(shí)性變量可以與永久變量同名循環(huán)語(yǔ)句循環(huán)語(yǔ)句 while 語(yǔ)句 forvalues 語(yǔ)句 foreach 語(yǔ)句條件循環(huán)語(yǔ)句:條件循環(huán)語(yǔ)句:while local j=0 while j5 dis aj local j=j+1 循環(huán)語(yǔ)句:循環(huán)語(yǔ)句: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個(gè)文件,分別為d1.dta,d2.dta,d100.dta 研究要求將這100個(gè)文件做縱向合并,寫出程序 use d1.dta,clear local i=2 forvalues i=2(1)100 append using di.dta save finaldata,replace循環(huán)語(yǔ)句:循環(huán)語(yǔ)句:foreach foreach var of varlist x y z command 示例1:將auto.dta各變量的對(duì)數(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) 條件語(yǔ)句:條件語(yǔ)句:if語(yǔ)法格式 格式1 if (條件) command 格式2 if(條件1) command1 else if(條件2) /這里可以沒有if command2 條件語(yǔ)句:條件語(yǔ)句:if示例示例 scalar aa=1 if aa=1 dis “這小子真帥!” else if aa=0 dis “這女孩真亮!” 引用引用stata命令的返回值命令的返回值 留存在內(nèi)存中的結(jié)果 r-class ,與模型估計(jì)無關(guān)的命令,如sum e-class, 與模型估計(jì)有關(guān)的命令,如regress s-class,其他命令, 如list c-class,存儲(chǔ)系統(tǒng)參數(shù) 顯示留存值的
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 抖音商戶直播投流ROI評(píng)估與優(yōu)化制度
- 全球生物制藥行業(yè)2025年創(chuàng)新藥研發(fā)管線與靶點(diǎn)深度報(bào)告
- 八大物流企業(yè)綠色物流實(shí)踐與行業(yè)規(guī)范制定報(bào)告
- 黑龍江中醫(yī)藥大學(xué)《藥用植物學(xué)實(shí)驗(yàn)》2023-2024學(xué)年第一學(xué)期期末試卷
- 公眾參與機(jī)制在2025年環(huán)境影響評(píng)價(jià)中的實(shí)踐與反思報(bào)告
- 2025屆江蘇省無錫市青陽(yáng)初級(jí)中學(xué)七年級(jí)數(shù)學(xué)第一學(xué)期期末監(jiān)測(cè)試題含解析
- 安慶醫(yī)藥高等專科學(xué)校《醫(yī)學(xué)微生物與臨床微生物檢驗(yàn)學(xué)》2023-2024學(xué)年第一學(xué)期期末試卷
- 內(nèi)蒙古自治區(qū)赤峰市翁牛特旗烏敦套海中學(xué)2024年九年級(jí)化學(xué)第一學(xué)期期末復(fù)習(xí)檢測(cè)試題含解析
- 2024-2025學(xué)年天津市河西區(qū)新華圣功學(xué)校九上化學(xué)期末復(fù)習(xí)檢測(cè)試題含解析
- 黑龍江幼兒師范高等專科學(xué)校《中國(guó)文化通論》2023-2024學(xué)年第一學(xué)期期末試卷
- 2025年6月22日四川省市直事業(yè)單位遴選筆試真題及答案解析
- 慶陽(yáng)市隴東學(xué)院招聘事業(yè)編制筆試真題2024
- 心理學(xué)考試題及答案
- 護(hù)理領(lǐng)域的職業(yè)發(fā)展與前景展望
- 2025年天津高考數(shù)學(xué)試卷試題真題及答案詳解(精校打印)
- 2025上海濟(jì)光職業(yè)技術(shù)學(xué)院輔導(dǎo)員考試試題及答案
- 2024年江蘇三支一扶真題
- 主、被動(dòng)防護(hù)網(wǎng)施工方案-圖文
- 2025年初中語(yǔ)文文學(xué)常識(shí):常考100題匯編
- 君易和文化課件
- 藥食同源106種25年4月更新
評(píng)論
0/150
提交評(píng)論