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   "Jason Becker" <[email protected]>
To   <[email protected]>
Subject   RE: st: a, perhaps, not so really silly question
Date   Tue, 8 Mar 2011 15:01:47 -0500

For a direct link to using a do-file with arguments, check out this FAQ:
http://www.stata.com/support/faqs/lang/args.html.

In my opinion, any series of analyses/routines that are not dataset
specific, and which I'm likely to run again, seems worth abstracting to
a do-file with arguments. I think it makes code more legible. For loops
can sometimes obscure precisely which set of variables I'm running the
same routine on if I'm reading quickly. 

I think I picked up on this practice from my one class on
object-oriented programming in college.  It seems more easily extensible
and reduces the space in my do-files that is dedicated to code that I've
stolen from myself, keeping only the elements unique to that data set or
routine in front of me at any given time.
___________________________________
Jason Becker
Research Specialist
Office of Data Analysis and Research
Rhode Island Department of Education
255 Westminster Street Providence, RI 02903
(401)-222-8495


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Nick Cox
Sent: Tuesday, March 08, 2011 2:21 PM
To: '[email protected]'
Subject: RE: st: a, perhaps, not so really silly question

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/

*
*   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