Transcription of program — Define and manipulate programs
1 Define and manipulate programsDescriptionSyntaxOptionsRemarks and examplesAlso seeDescriptionprogram definedefines and manipulates required ifprogramnameis anyof the words:define,dir,drop,list, dirlists the names of all the programs stored in listlists the contents of the named program or listallliststhe contents of all programs stored in dropeliminates the named program or programs from dropalleliminates all programs stored in dropalladoeliminates all programs storedin memory that were loaded from ado-files. See[U] 17 Ado-filesfor an explanation of [U] 18 Programming Statafor a description of programs . The remarks below address onlythe use of theprogram dir, program drop, andprogram [P]tracefor information on debugging theCombined subject table of contents, which immediately follows theContents, for a subjectsummary of the programming programprogram[define]programname[,[ncla ss|rclass|eclass|sclass]byable(recall[, noheader]|onecall) properties(namelist) sortpreserveplugin]List names of programs stored in memoryprogram dirEliminate program from memoryprogram drop{programname[programname[.]]}
2 ]]|all|allado}List contents of programprogram list[programname[programname[..]]|all]Op tionsnclassstates that the program being defined does not return results inr(),e(), ors(), and is program Define and manipulate programsrclassstates that the program being defined returns results inr(). This is done using thereturncommand; see [P]return. If the program is not explicitly declared to berclass, then it may notchange or replace results inr().eclassstates that the program being defined returns results ine()or modifies already existingresults ine(). This is done using theereturncommand; see [P]returnand [P]ereturn. If theprogram is not explicitly declared to beeclass, it may not directly replace or change results ine()sclassstates that the program being defined returns results ins().
3 This is done using thesreturncommand; see [P]return. If the program is not explicitly declared to besclass, then it may notdirectly change or replace results ins(), but it still may clears()by usingsreturn (recall[, noheader]|onecall)specifies that the program allow Stata are two styles for writing byable programs :byable(recall)andbyable(onecall ). Thewriting of byable programs is discussed in [P] (namelist)states thatprogramnamehas the specified containup to 80 characters, including separating spaces. See [P] program that the program changes the sort order of the data and that Stata is to restorethe original order when the program concludes; see [P] that a plugin (a specially compiled C program ) be dynamically loaded and that theplugin define the new command ; see [P] and dircommand lists the names of all the programs stored in listlists contents of the program or 1 When you start Stata, there are no programs stored in memory.
4 If you typeprogram dir, Statadisplays an empty list:. program Define and manipulate programs 3 Later during the session, you might see. program dir(output omitted)ado 756 _pred_seado 644 306 5296 logit_pado 339 predict(output omitted)ado 559 4272 827 logitado 287 588 269 686 webuse118187 Theadoin front indicates that the program was automatically loaded and thus can be automaticallydropped should memory become scarce; see[U] 17 Ado-files. The number is the size, in bytes,of the program . The total amount of memory occupied by programs is 114,306 bytes. Notice These programs are defined in and were loaded whenlogitpwas s now create two of our own programs withprogram.
5 program rng1. args n a b2. if " b "=="" {3. display "You must type three arguments: n a b"4. exit5. }6. drop _all7. set obs n 8. generate x = (_n-1)/(_N-1)*( b - a )+ a 9. end. program smooth1. args v1 v22. confirm variable v1 3. confirm new variable v2 4. generate v2 = cond(_n==1|_n==_N, v1 ,( v1 [_n-1]+ v1 + v1 [_n+1])/3)5. end4 program Define and manipulate programsAfter you typeprogram, lines are collected until you type a line with the wordend. For our purposes,it does not matter what these programs do. If we were now to typeprogram dir, we would see. program dir286 smooth319 rng(output omitted)ado 756 _pred_seado 644 306 5296 logit_pado 339 predict(output omitted)ado 559 4272 827 logitado 287 588 269 686 webuse118792We can list a program by using theprogram listcommand.
6 program list smoothsmooth:1. args v1 v22. confirm variable v1 3. confirm new variable v2 4. generate v2 = cond(_n==1|_n==_N, v1 ,( v1 [_n-1]+ v1 + v1 [_n+1])/3)If we do not specify the program that we want listed, program listlists all the programs stored Define and manipulate programs 5 Theprogram dropcommand eliminates programs from memory. Typingprogram droppro-gramnameeliminatesprogramnamefro m memory. Typingprogram dropalleliminates allprograms from program drop smooth. program dir319 rng(output omitted)ado 756 _pred_seado 644 306 5296 logit_pado 339 predict(output omitted)ado 559 4272 827 logitado 287 588 269 686 webuse118506. program drop _all.
7 program see[P]byable Make programs byable[P]discard Drop automatically loaded programs [P]sortpreserve Sort within programs [P]trace Debug Stata programs [D]clear Clear memory[R]query Display system parameters[U] 18 Programming Stata