Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

[no subject]



I wouldn't do it like that. I would have an option

periods(str)

and set up a little language, say

periods(x2=1980(1)2000 x3=1998&1990)

Then you parse the argument of -periods()-
ad hoc, within your program, downstream of -syntax-.

There is a trade-off between being restrictive
over the details of the little language (making
life easier for the programmer) and being relaxed
(making life easier for the user).

For example, you could insist that spaces are
used to separate specifications for each variable,
and only for that purpose. That would mean that
you would need syntax for your second example,
say using "&" as above.

So, a sketch could be

if "`periods'" != "" {
	foreach p of local periods {
		local whereeq = strpos("`p'", "=")
		if `whereq' == 0 { 	
			di as err "invalid syntax in periods()"
			exit 198
		}
		else {
			local var = substr("`p'",1,`whereq'-1)
			capture confirm var `var'
			if _rc {
				di as err "`var' not a variable in periods()"
				exit <suitable error code>
			}
			local spec = substr("`p'",`whereq'+1,.)
			local spec : subinstr local spec "&" " ", all
			numlist `spec'
			local spec "`r(numlist)'"
			// put `var' and `spec' somewhere for later use
		}
	}
}

Nick
n.j.cox@durham.ac.uk

Tom Boonen

> I am fairly new to Stata programming and have a question regarding the
> best way to use the syntax command to parse user input into macros so
> I can work with them in my program.
>
> In my estimation command, the user enters a response variable Y and
> several predictor variables X1 X2 X3.
>
> so this is easy to parse using
>
> syntax varlist
> gettoken depvar varlist: varlist
> ...
>
> So the user would code:
>
> command Y X1 X2 X3
>
> Now, I also would like to give the user the possibility to specify for
> each predictor a numlist of periods over which the predictor shall be
> used in the estimation (this is kind of unusual, but makes sense in my
> particular estimation). So the user would code something like:
>
> command Y X1 X2(1980(1)2000) X3(1998 1990)
>
> which would mean that X1 would be used for all years. X2 would be used
> for years 1980,1981,...,2000 and X3 would be used for years 1998 and
> 1990 only. I wonder what would be the best way to parse this. I was
> thinking:
>
> syntax anything
>
> then
> gettoken depvar varlist: varlist
>
> then i would need to loop through each element of varlist, look for
> the first "(" , obtain the numbers after the "(", look for the next
> ")", store the number in front of it. etc etc etc.
>
> this seems a bit tedious and error prone (the user may put an extra
> blank or number here and there). i wonder whether there is any better
> way to parse this?

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


On 3/8/07, Tom Boonen <tom.boonen.maiden@gmail.com> wrote:
> Hi,
>
> I am fairly new to Stata programming and have a question regarding the
> best way to use the syntax command to parse user input into macros so
> I can work with them in my program.
>
> In my estimation command, the user enters a response variable Y and
> several predictor variables X1 X2 X3.
>
> so this is easy to parse using
>
> syntax varlist
> gettoken depvar varlist: varlist
> ...
>
> So the user would code:
>
> command Y X1 X2 X3
>
> Now, I also would like to give the user the possibility to specify for
> each predictor a numlist of periods over which the predictor shall be
> used in the estimation (this is kind of unusual, but makes sense in my
> particular estimation). So the user would code something like:
>
> command Y X1 X2(1980(1)2000) X3(1998 1990)
>
> which would mean that X1 would be used for all years. X2 would be used
> for years 1980,1981,...,2000 and X3 would be used for years 1998 and
> 1990 only. I wonder what would be the best way to parse this. I was
> thinking:
>
> syntax anything
>
> then
> gettoken depvar varlist: varlist
>
> then i would need to loop through each element of varlist, look for
> the first "(" , obtain the numbers after the "(", look for the next
> ")", store the number in front of it. etc etc etc.
>
> this seems a bit tedious and error prone (the user may put an extra
> blank or number here and there). i wonder whether there is any better
> way to parse this?
>
> any help is appreciated.
>
> thanks,
> tom
>
> gettoken depvar varlist: varlist
>
> Y
>
>
>  but for each predictor the user may specify a ceratin time period
> over which the respective predictor is averaged.
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index