Statalist The Stata Listserver


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

RE: st: parsing question


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: parsing question
Date   Fri, 9 Mar 2007 15:09:45 -0000

Your correction looks correct. 

Another way to do it is by allowing a set of options
-- say -period1()- to -period9()- -- as many as you might 
need. 

You could then be more indulgent. The syntax could then be 
(e.g.) 

period1(x2 1980(1)2000) period2(x3 1998 1990)

forval i = 1/9 { 
	if "`period`i''" != "" {
		gettoken var spec : period`i'

		capture confirm var `var'
 		if _rc {
 			di as err "`var' not a variable in period`i'()"
			exit <suitable error code>
		}

		numlist "`spec'" 
		local spec "`r(numlist)'" 
		// put `var' and `spec' somewhere for later use
	}
}

Nick 
[email protected] 

Tom Boonen
 
> I really like your idea of using periods(str) and a synatx like
> 
> periods(x2=1980(1)2000 x3=1998&1990)
> 
> while imposing some discipline on the user by insisting that spaces
> are used to separate specifications for each variable. I walked
> through your program sketch and it looks exactly like what I need.
> Some of the string functions are new to me and they look very helpful.
> Thanks a lot.
> 
> Tom
> 
> PS: just on tiny correction I think numlist `spec' needs to be numlist
> "`spec'" for spec to be transformed into a numlist.
> 
> 
> 
> 
> 
> From 	  "Nick Cox" <[email protected]>
> To 	  <[email protected]>
> Subject 	  st: RE: parsing question
> Date 	  Fri, 9 Mar 2007 10:19:56 -0000
> 
> 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
> [email protected]
> 
> 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/



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