NetCourse 151 An Introduction to Stata Programming.doc
《NetCourse 151 An Introduction to Stata Programming.doc》由会员分享,可在线阅读,更多相关《NetCourse 151 An Introduction to Stata Programming.doc(163页珍藏版)》请在三一办公上搜索。
1、Lecture 1 - Table of ContentsWelcomeEntering and executing a programMechanical method 1: The do-fileAside: Built in do-file editorMechanical method 2: the interactive program commandMechanical method 3: program in a do-fileMechanical method 4: combination do-filesMechanical method 5: ado filesAside:
2、 Using Stata and an editor or word processorOrganizing do-filesAn individual do-fileA do-file to perform verificationInfiling dataAside: Working with datasets that are too largeReproducibilityIndexingassert as an alternative to branchingConsuming calculated resultsExercisesConclusionAppendix A: Samp
3、le data for hierarchical dataset exampleAppendix B: Sample data for relation example WelcomeWelcome to NetCourse 151 - An Introduction to Stata Programming. Before we get started there are a few things we need to get out of the way. In the NetCourse Basics message we showed those with a web-aware St
4、ata how to use Stata to automatically download the important cutouts for each lecture. You will want to do that now for lecture 1. For instance, within Stata you would move to your course directory (using the cd command), create a directory for this lecture, and download the files. Here is what I di
5、d: . cd C:nc151 . mkdir lecture1 . cd lecture1 . run Reread the NetCourse Basics message for more details on this command. In this lecture we discuss How to enter a program and execute it Programming as automating data management and analysis The importance of organization, especially as it pertains
6、 to reproducibility The importance of data verification Simple data checks and debugging (assert and trace) Working with datasets that are too large to fit in memory Reading a hierarchical datasetBack to Table of ContentsEntering and executing a programThis is a course about programming Stata, but b
7、efore we can get into the details of programming, you need to master the mechanics of programming. You need to learn how to enter a program into Stata and how to get Stata to execute it. A famous book on programming began with a program to do nothing more than display the text Hello, world. This was
8、 clever because it eliminated all programming complexity; However, that still left the considerable complexity of dealing with the compiler. We will do the same thing by writing a program with a body of . display Hello, worldHello, worldWe will deal more with the display command later, but right now
9、, when you see display Hello, world, you are supposed to imagine that it stands for something longer and more elegant. Then you should ignore that fact and look at all the Stata junk around it - the stuff that turned the body display Hello, world into a program that can be executed over and over aga
10、in. Typographical note: From now on, commands issued to Stata will be in bold black text. Back to Table of ContentsMechanical method 1: The do-fileA do-file is just an ASCII file (plain text file with no special characters) containing Stata commands that you create with an editor or word processor.
11、When you interactively type do filename at the keyboard, the contents of file are executed just as if you typed each line at the keyboard. BEGIN hello.dodisplay Hello, worldEND hello.doYou run this program by interactively typing . do hello-you type this. display Hello, world-Stata types thiswhich d
12、isplays the following in the Stata Results window Hello, world.end of do-file . Save As. Name the new file hello.do, and make sure that you save it in your current (working) directory. Now, either close the do-file editor (click on the X), or push it aside. In the Stata command window, type do hello
13、 and press Enter.Although this seems simple enough, it may not work when you try it. There is a lot, mechanically, that can go wrong (which is why you should try it - it will be easier to master these problems now, with this one-line do-file, than to wait and face the problems in a complicated case)
14、. What might go wrong: You enter your word processor or editor, enter hello.do, and save it. You try to do it from Stata and are told file hello.do not found. o Solution: hello.do is not in the current directory. Either copy the file to the current directory using your operating system, or use cd to
15、 change to the directory containing the do-file. Another possibility: it is in the current directory, but you did not name it hello.do. Say you named it hello.pgm. Then you would execute it by typing do hello.pgm. Say you named it simply hello. Then you would execute it by typing do hello. (note: th
16、e period is part of the command). Stata do-files have a default suffix: .do When you type do X, Stata assumes you mean do X.do. If the suffix is other than the default, you must specify it. If there is no suffix, you put a period at the end. File hello.do is found, but what is displayed is nothing l
17、ike what you entered when you do it. o Solution: You did not save hello.do as an ASCII file, but saved it as some sort of document. If you use a word processor to enter do-files, you must save them as ASCII files. File hello.do is found, it appears okay, but it does not do anything. That is, when yo
18、u run it you see . do hello . display Hello, world Note that Stata executed the display command but never displayed Hello, world. o Solution: You forgot to end the single line in the do-file with a hard return. The line is not terminated, so Stata ignored it. Go back and add the return. Make sure to
19、 save your file. Perhaps something else could happen; if so, email us at private151. In the meantime, if you ran into this last problem, we have a suggestion: end all of your do-files with the word exit. exit, in a do-file, does not exit Stata but exits the do-file. Thus, we recommend that you enter
20、 your do-file as BEGIN hello.dodisplay Hello, worldexitEND hello.doThere are now two possibilities: either you remembered to put the hard return after the exit, or you did not. Either way, it will not matter. If you remembered, Stata will see the command exit and exit the do-file. If you did not, St
21、ata will not see exit, the do-file will end, and Stata will still exit the do-file. With the exit command in place, you could not have forgotten the hard return at the end of display Hello, worldbecause, if you had, that command would display in your word processor as being run together with the exi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- NetCourse 151 An Introduction to Stata Programming
链接地址:https://www.31ppt.com/p-2388927.html