简体   繁体   English

Stata中的程序,用于区分文件类型(数据集和文件)

[英]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.

相关问题 Stata:使用局部变量在Stata中构建程序 - Stata: using local variables to build a program in Stata 批处理文件帮助请:如果存在C:\\ Program Files \\ Adob​​e或C:\\ Program Files(x86)\\ Adob​​e - Batch file help please: If exist C:\Program Files\Adobe OR C:\Program Files (x86)\Adobe 批处理文件:检查程序,重新启动或不执行任何操作 - Batch file: Check for program, restart, or do nothing 如何使用批处理文件中不存在的文件在程序文件文件夹中搜索文件 - How to search a file in program files folder using if not exists in batchfile Powershell将文件循环到程序中,如果退出代码不等于0,则将文件移动 - Powershell loop files into program, if exit code is not equal to 0 move the file 如果语句Stata - If statement Stata 如何通过google驱动器的链接找到文件的类型? - How do I find the type of the file through the link of the google drive? 创建某些文件类型的文件名列表,如果文件夹不包含文件类型,则打印此语句 - Create list of files names of certain file types, if folder does not contain file type, print this statement Stata:如果有则替换 - Stata: replace, if, forvalues 条件if和编程如果在Stata中 - conditional if and programming if in Stata
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM