Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: a, perhaps, not so really silly question


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: a, perhaps, not so really silly question
Date   Tue, 8 Mar 2011 19:20:33 +0000

Scott T
=======

I gather the difference between a 'program' and a 'do file' is that 
programs are stored in memory and do files in a default directory.  In a 
general sense they're both "programs" in that they're stored lists of 
executable instructions.

Nick Cox
========

Not wrong in all respects, but not aiming in the right direction either. If we are talking about definitions, here are mine. 

(1) A program in Stata is, strictly, something that is defined by a -program- command. Usually, a program is included in an .ado file, although that is not compulsory. You can define programs interactively, for example, although that is rarely done except with short ones. 

(2) A do file is whatever is run by a -do- or -run- command. In a wide sense, this includes part or all of the contents of a do-file editor. In a narrow sense, the file extension .do is customary, but not compulsory. 

There is a small grey area in that you can define a program by running a do file containing a program, but as that is using (2) to declare (1) to Stata it is not an exception really. 

All documented in [U] 16-18. 

You are free to think broad sense -- indeed I occasionally stress that any interactive session is in a way writing a Stata program line by line -- but not learning and following Stata's terminology is more likely to slow understanding and impede communication. 

Scott T
=======

Another typical problem I have that's a bit more complicated than the 
"tab" example involves executing a series of commands on pairs of 
variables.  There is lots of discussion on nested "for" loops, but how 
would one *not* nest the loops?  Specifically I want to execute the 
following list of commands on paired variables x and y:

tabxml, dv(`y') ivd(rchurch) options(n) split(mono) osvy save( 
C:\data\NLSY Children of 79\Deliverables/`x'IN)
tabxml, dv(`y') ivd(rchaste) options(n) split(mono) osvy save( 
C:\data\NLSY Children of 79\Deliverables/`x'IIN)
tabxml, dv(`y') ivd(rchurch) options(per) tf(2) split(mono) percent(row) 
osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IP)
tabxml, dv(`y') ivd(rchaste) options(per) tf(2) split(mono) percent(row) 
osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIP)
tabxml, dv(`y') ivd(churchx) options(per nototal) tf(2) split(rprom) 
percent(row) osvy save( C:\data\NLSY Children of 79\Deliverables/`x'IIIPa)
tabxml, dv(`y') ivd(churchx) subcond(mono==1) options(per nototal) tf(2) 
split(rprom) percent(row) osvy save( C:\data\NLSY Children of 
79\Deliverables/`x'IIIPb)
tabxml, dv(`y') ivd(churchx) subcond(mono==0) options(per nototal) tf(2) 
split(rprom) percent(row) osvy save( C:\data\NLSY Children of 
79\Deliverables/`x'IIIPc)

What I've been doing is manually replacing the items in quotations above 
the commands and just running the same do file over and over, but it's 
getting kind of tedious for 20 variable pairs.

loc x="DroppedOut"
loc y="drop"

then replace with

loc x="RepeatedGrade"
loc y="rgrade"

and run again, etc.  Seems like I should  be able to use a for loop with 
variable pairs instead of single variables, but it just escapes me how 
to do it.  Does no one ever want to do something like this?

Nick Cox
========

Yes, many people want to do something like this. The question is whether the repeated structures are regular enough to make heavy rewriting of it worthwhile. You have the main idea that you can use locals to define repeated text, but it's hard to see a structure in your example that lends itself to more abstraction. 

For example, your -ivd()- calls run 

ivd(rchurch) 
ivd(rchaste) 
ivd(rchurch) 
ivd(rchaste) 
ivd(churchx) 
ivd(churchx) 
ivd(churchx) 

Tastes and styles vary, but in my not especially humble opinion there is little point to making a do-file shorter if you also make it in total more difficult to read. In practice, the key is not what a do-file looks like but whether you have the text editor skills to make writing and maintaining it as fast as possible. 

But as I said earlier do files taking arguments is something you should look at. 



*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index