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]

st: RE: RE: RE: Isolate the 'if statement' from a regression command


From   Nick Cox <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
Subject   st: RE: RE: RE: Isolate the 'if statement' from a regression command
Date   Mon, 28 May 2012 15:31:33 +0100

Much depends on whether you know in advance that the command being processed is of a very predictable and/or simple form. At the simplest if you know that an -if- condition will be last then you can search for that, but you could always run into problems if "if" are the last two characters of a variable name, and so on, and so forth. 

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


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Lythgoe, Dan
Sent: 28 May 2012 15:25
To: 'statalist@hsphsun2.harvard.edu'
Subject: st: RE: RE: Isolate the 'if statement' from a regression command

Nick many thanks for your helpful response - this is probably the way to go. An inelegant alternative inspired by your use of -subinstr- seems to work OK too:

reg price mpg headroom if foreign==1
local 0 `e(cmdline)'
local cmd `e(cmd)'
local depvar `e(depvar)'
local 0 : subinstr local 0 "`cmd'" "" 
local 0 : subinstr local 0 "`depvar'" "" 

foreach var of varlist mpg headroom {
	local newlist : subinstr local 0 "`var'" ""
	local 0 "`newlist'"

}
di "If statement: " "`0'"

Obviously the varlist can be isolated in a simlar manner to pass to the 'foreach var' loop and any regression model options can be easily isolated using:

tokenize "`0'", parse(",")

...although this should be done before removing the other elements.

Kind regards
Dan


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox
Sent: 28 May 2012 14:32
To: 'statalist@hsphsun2.harvard.edu'
Subject: st: RE: Isolate the 'if statement' from a regression command

You may find it easiest to push the command through -syntax-. Although it's not emphasised very much (that I can recall), -syntax- doesn't require the context of a previous -program- call, although that is its natural habitat. -syntax- works on whatever is in local macro 0. 

Thus in this case the principle is shown by 

reg price mpg headroom if foreign==1
local 0 `e(cmdline)'
local 0 : subinstr local 0 "regress" "" 
syntax varlist [if] [in] [, * ] 
mac li

Notes: 

1. Watch out. -syntax- zaps whatever is in the local macros it might create. It may be better to write a small subroutine that does the processing for you and returns the -if- element. Using -c_local- (no; it's not documented) or using s-class results is natural here. 

2. You may of course need something more elaborate. This is just a pointer. 

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


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Lythgoe, Dan
Sent: 28 May 2012 14:07
To: 'statalist@hsphsun2.harvard.edu'
Subject: st: Isolate the 'if statement' from a regression command

Dear statalist,

Is there a simple way to isolate the 'if statement' from a regression command line for storage as a local macro? Ideally we could do with being able to parse using "if" in -tokenize-, but parse() only accepts single characters and instead parses on "i" and "f":

Sysuse auto

reg price mpg headroom if foreign==1
local cmdline `e(cmdline)'
tokenize "`cmdline'", parse("if")
di "`1'" " | " "`2'" " | " "`3'"


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