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
Scott Talkington <[email protected]>
To
[email protected]
Subject
Re: st: a, perhaps, not so really silly question
Date
Tue, 08 Mar 2011 13:52:35 -0500
On 3/8/2011 4:04 AM, Nick Cox wrote:
Sorry. Premature send.
I don't see why you want to write a program to tabulate two variables
when you can just type
. tab x y
But here is one.
program mytab
version 8.2
tabulate `0'
end
mytab x y
That just lets -tabulate- do all the work. But that's reasonable
program style.There is no need for your program to parse a variable
list, -if-, -in-, weights, options, when -tabulate- does all that
fine.
Local macro 0 contains whatever you typed after a program name. That
is explained somewhere in the documentation.
But help for -syntax- tells you how to set it up the long way if you
do want to do this.
program mytab
syntax varlist [if] [in] [fweight aweight] [, * ]
You can add other stuff. If you want to work separately on the
variables concerned, you should look at -tokenize- and -args-. Given
syntax varlist(min=2 max=2)
...
tokenize "`varlist'"
args x y
your two variables' names are put in local macros x and y.
I wouldn't call what this program does a loop, but Scott may well have
been writing loosely.
I agree with Scott that there is no single help file or FAQ that is
going to tell you how to write a program. That is the job of the
manual and it takes more than a little space. A couple of chapters in
[U] are the place to start, followed by [P] as needed, but once you've
got going you tend to steal from other programs, including your own.
In short, it's all explained, but you have to give yourself a bit of
time to work it out.
Another take is that Scott thinks he wants a program, but a do file
that takes arguments sounds equally close to his purpose. Despite
previous programming experience, my first couple of years of Stata use
were dominated by writing such do files. Now I find it more natural to
write programs straight off, but plenty of users do really good work
without ever typing the word -program-.
Nick
Nick:
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.
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?
On Tue, Mar 8, 2011 at 2:46 AM, Scott Talkington<[email protected]> wrote:
The stuff that stumps me in stata is usually something so simple that it's
impossible to find a help file or a faq that deals with it in simple terms.
I want to write a program that obtains a variable name from keyboard input
and then executes a series of commands on that variable. This should be
pretty simple programming but I can't find a simple description of it.
lets say I want to fill a local variable `x' with one variable name and a
local variable `y' with another variable name than then execute the command
.tab `x' `y'
and then at the end of the loop it just stops and waits for the keyboard
input again
*
* 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/