NetCourse 151 An Introduction to Stata Programming.doc

上传人:laozhun 文档编号:2388927 上传时间:2023-02-17 格式:DOC 页数:163 大小:1.11MB
返回 下载 相关 举报
NetCourse 151 An Introduction to Stata Programming.doc_第1页
第1页 / 共163页
NetCourse 151 An Introduction to Stata Programming.doc_第2页
第2页 / 共163页
NetCourse 151 An Introduction to Stata Programming.doc_第3页
第3页 / 共163页
NetCourse 151 An Introduction to Stata Programming.doc_第4页
第4页 / 共163页
NetCourse 151 An Introduction to Stata Programming.doc_第5页
第5页 / 共163页
点击查看更多>>
资源描述

《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

22、t command: display Hello, worldexitBack to Table of ContentsMechanical method 2: the interactive -program- commandAnother way to enter our program would be to interactively type . program hello 1. display Hello, world 2. endand when we want to execute it, type . helloHello, worldDo this. You have ju

23、st created your first Stata command. You will seldom want to define your programs interactively, but it is easier to learn what a command can and cannot do for us by trying it interactively. As with do-files, things can go wrong: Problem 1: program redefinition. You want to change the program hello

24、to display Hi back rather than Hello, world. You type: . program hello hello already defined r(110); Stata remembers definitions throughout the session, so you cannot redefine a program until you drop the old version: . program drop hello. program hello 1. display Hi back 2. end. helloHi back. Probl

25、em 2: program does not know about built-in command names. Lets enter our Hello, world program and call it b: . program b 1. display Hello, world 2. end. bHello, world Fine, that works. Now lets call it d: . program d 1. display Hello, world 2. end. dContains data obs: 0 vars: 0 size: 0 (99.9% of mem

26、ory free) Sorted by: What happened? d is the Stata abbreviation for the built-in Stata command describe. program let us define a program called d, but there is no way we can execute it because Statas built-in commands take precedence. How can you find out if a name is already taken? Use Statas which

27、 command: . which d built-in command: describe . which b command b not found as either built-in or ado-file r(111); Problem 3: program does not check syntax. The syntax of program is program program-name 1. content of program more program etc. 2. end Between the program and the end, Stata merely sto

28、res whatever you type. Stata does not determine whether what you type makes sense until you try to execute it. . program hello 1. display Hello, world 2. end. helloHello not foundr(111); In this case, we omitted the double quotes around Hello, world. In our one-line program, finding the error is eas

29、y. Where else could it be than on line 1? If our program were 50 lines long, finding it would be more difficult. Stata can trace the execution of a program: . set trace on. hello- display Hello, worldHello not foundr(111);. set trace off One thing to remember: if you set trace on, do not forget to t

30、urn it back off afterwards. Otherwise, youll run some other Stata command and be surprised by the amount of output produced. Hints for debugging large, complicated programs: . set trace on /* turn on tracing */. set more off /* turn off -more- */. log using junk, replace /* start a log */. invoke pr

31、ogram Output will scroll by without -more- ever appearing. Eventually, the error will occur. Then, . log close. set more on /* turn back on -more- */. set trace off /* turn off trace */ Now you can look at junk.log using the Viewer. Problem 4: theres a limit to how big a program can be. A single pro

32、gram program must contain fewer than 3,500 lines and less than 135,600 characters in Intercooled Stata and Stata/SE. In Small Stata the limit is 1,000 lines. Actually, the Intercooled Stata and Stata/SE limit is 135,600 characters, and the Small Stata limit is 37,296 characters after a little compre

33、ssion Stata performs. This, in practice, is not a problem because a) 3,500 lines are a lot, andb) programs can call other programs. Nevertheless, we sometimes write programs that are too big and then have to go back and split them into pieces. Problem 5: there is no way to edit a program after you h

34、aver entered it. You can define programs interactively, you can execute programs interactively, and you can drop programs interactively, and that is it. Not only can you not edit the program, but there is no way to store it. This makes the interactive definition of a program not useful. Note that by

35、 interactively, we mean at the command line prompt. Think of the Stata do-file editor as a separate thing. Back to Table of ContentsMechanical method 3: -program- in a do-fileIf program can be used interactively, and if do-files execute lines as if they were entered interactively, then do-files can

36、contain programs. The advantage, of course, is that because you enter do-files using your editor or word processor, you can edit programs and store them. BEGIN hello.doprogram hellodisplay Hello, worldend exitEND hello.doSo, lets try it: . do hello- we type interactively . program hello- Stata respo

37、nds hello already defined r(110); end of do-file r(110);Why didnt that work? Because we have already defined a program called hello (in preparing this lecture, we have been working in Stata interactively, and hello is left over from a previous example). So, lets drop the hello program and try again:

38、 . program drop hello- we type interactively . do hello- we type . program hello- Stata types 1. display Hello, world 2. end . exit end of do-fileLook carefully at what just happened: the program hello was not executed. Do-files contain lines that Stata executes as if you typed them in from the keyb

39、oard, and our do-file contains BEGIN hello.doprogram hellodisplay Hello, worldend exitEND hello.doNowhere in the do-file do we call on hello to be executed, so Stata does not execute it. In this case, because our program is now loaded, we can execute it interactively: . hello Hello, worldIn do-files

40、 that merely define programs, we typically do not want to see the program lines scroll by when we load them. It is more convenient to load such programs using run, which is the same as do but suppresses the output: . program drop hello - we type, so we can reload. run hello - we type, hello.do runs

41、silently . hello Hello, worldBack to Table of ContentsMechanical Method 4: combination do-filesDo-files execute whatever commands we include in them. We do not have to merely define our program in the do-file - we could define our program and run it, or define numerous programs, or define numerous p

42、rograms and run them. Anything we can do interactively - thats almost everything Stata can do - we can do in a do-file. So, here is our do-file modified to load and execute our program: BEGIN hello.doprogram hellodisplay Hello, worldend helloexitEND hello.doNow we can interactively type . do hello-

43、we type . program hello- Stata responds hello already defined r(110); end of do-file r(110);Oops! . program drop hello- we type . do hello- we type . program hello- Stata responds 1. display Hello, world 2. end . hello Hello, world. exit end of do-file .- our turn to type againHaving to remember to type program drop hello is tedious. Since do-files execute whatever we put in them just as if we were typing from the keyboard, we can put the program drop into our do-file: BEGIN hello.doprogram drop helloprogram hellodisplay Hello, worldend

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 项目建议


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号