[英]Program in Stata that differentiates file type (datasets and do-files)
I am trying to write a simple program in Stata where my students can download datasets and example do files from my website. 我正在尝试在Stata中编写一个简单的程序,让学生可以从我的网站下载数据集和示例文件。 This program is called "usecda" and below is the example where it only opens datasets. 该程序称为“ usecda”,下面是仅打开数据集的示例。
capture program drop usecda
program define usecda
args data
use https://tamaravdd.github.io/icpsrcda/`data', clear
end
Then I can do: 然后我可以做:
usecda hrs_subset.dta
I would like the same program to run do-files. 我希望同一程序运行do-files。 How I differentiate the do-files from dataset file in the program (using an if-statement I assume)? 我如何区分程序中的do文件和数据集文件(使用假定的if语句)? I am open to either differentiate them by file type (.dta or .do) or by using a term in the name file (the do-files all have "lecture" in them). 我愿意通过文件类型(.dta或.do)或通过在名称文件中使用术语来区分它们(do文件中都包含“演讲”)。
This program would be able to do the usecda hrs_subset.dta
to open the dataset and also usecda cda_lecture_nominal.do
to run the do file 该程序将能够执行usecda hrs_subset.dta
来打开数据集,并能够使用usecda cda_lecture_nominal.do
来运行do文件。
Thanks for your help! 谢谢你的帮助!
Here is something for you to work off of: 这是您需要做的事情:
capture program drop usecda
program define usecda
args data
// check for .dta file
if substr("`data'",length("`data'")-3,4) == ".dta" {
//use "https://tamaravdd.github.io/icpsrcda/`data'", clear
di "dta file"
}
// check for .do file
else if substr("`data'",length("`data'")-2,3) == ".do" {
//do "https://tamaravdd.github.io/icpsrcda/`data'"
di "do file"
}
// throw error if not .dta or .do
else {
di as err "File `data' incorrectly specified"
}
end
usecda asdf.dta
usecda asdf.do
usecda asdf.doc
the idea is to check the file extension and if it is a .dta file then do something, if it is a .do file do something else, and if it is not either of those then throw an error to the user. 这个想法是检查文件扩展名,如果它是.dta文件,则执行某些操作;如果是.do文件,则执行其他操作;如果不是,则向用户抛出错误。
I included examples of each under the program definition. 我在程序定义下列出了每个示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.