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: really silly question
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: really silly question
Date
Tue, 8 Mar 2011 09:04:17 +0000
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
> 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/