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: Isolate the 'if statement' from a regression command


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: Isolate the 'if statement' from a regression command
Date   Mon, 28 May 2012 14:31:36 +0100

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 
[email protected] 


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Lythgoe, Dan
Sent: 28 May 2012 14:07
To: '[email protected]'
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'"

Kind regards
Dan

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index