Statalist


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

st: RE: Local in -if expressions


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Local in -if expressions
Date   Tue, 18 Mar 2008 20:02:10 -0000

The problem here is not to do with -if- or -in- as such. 
It is a matter of handling string comparisons. 

As an example, let us suppose that the program branched to 

local cons constant
local tr notrend

and you then try 

if (`cons'=="noconstant") & (`tr'="notrend") {
	
A key rule here is: substitute macro names by their contents. So Stata 
sees 

if (constant =="noconstant") & (notrend ="notrend") {

That first comparison makes sense if you have a variable called
-constant- (or for 
that matter a string scalar with the same name). But you evidently
don't; so 
Stata complains and throws you out. 

What you want is to compare a string with a string, so the code should 
be 

if ("`cons'"=="noconstant") & ("`tr'"="notrend") {

In fact there is an extra bug in this line: it just has not bitten you
yet. 
You need == twice, not once. 

if ("`cons'"=="noconstant") & ("`tr'"=="notrend") {

I like to put spaces around most binary operators, but that's purely a
matter of taste, 
apart from any discussion of readability. 

if ("`cons'" == "noconstant") & ("`tr'" == "notrend") {

There may be other problems: I just focused on what should be generating
the error message. 

Nick 
[email protected] 

Paulo Regis

I am having a problem with local macros. Whenever I need to use the
-if command, I cannot include in the expression the local. This is not
the first time I had this kind od problem but I did not find a way
aroud this time.

For example, I have 5 dependent (y1-y5) and independent (x1-x5)
variables, a trend (created by the code, and included as a variable
named "trend") and I want to check for cointegration of each dependent
on each independent variable, considering 3 alternative specifications
1_ No constant, No trend
2_ constant, No trend
3_ constant, trend

The programme (which I include at the end of the email) selects the
specification with the highest Zobs (this is a simplification,
actually, i base the selection on AIC). At the end of the programme, I
need to post the Zobs (i.e. sample statistic) together with the Zcrit
(i.e. critical value). The critical values are from Mackinnon (1990),
testing for cointegration.

My problem is at the end of the code. I get the error:
constant not found
r(111);

The three -if with locals in the expression, which I reproduce :
if (`cons'=="noconstant") & (`tr'="notrend") {
scalar Zcrit== -3.98
}
if (`cons'=="constant") & (`tr'="notrend") {
scalar Zcrit== -3.86
}
if (`cons'=="constant") & (`tr'="trend") {
scalar Zcrit== -3.39
}

What am I doing wrong? (Please, take in mind my code is much more
complicated and I cannot check the Zobs and Zcrit by hand. Also, the
if blocks include other commands to perform some complementary checks)

Cheers

Paulo Regis

* COMPLETE PROGRAMME:
capture postclose Posting
capture drop trend
capture drop r
postfile Posting DepVar IndepVar Zobs Zcritcons Constant trend using
"C:\LongRun_VEC.dta", replace
global DepVar "y1 y2 y3 y4 y5"
global Var "X1 X2 X3 X4 X5"
gen trend=_n
tsset trend
foreach x of global Var {
foreach y of global DepVar {
 regress `y' `x'
 predict r, residuals
 *No Constant and No Trend
 dfuller r, noconstant
 scalar Zobs=r(Zt)
 local cons noconstant
 local tr notrend
 *Constant and No Trend
 dfuller r,
 scalar Zobs2=r(Zt)
  if Zobs2<Zobs2{
  scalar Zobs=Zobs2
  local cons constant
  local tr notrend
  }
 *Constant and Trend
 dfuller r, trend
 scalar Zobs2=r(Zt)
  if Zobs2<Zobs{
  scalar Zobs=Zobs2
  local cons constant
  local tr trend
  }
 dfuller r, `cons' `tr'
  if (`cons'=="noconstant") & (`tr'="notrend") {
  scalar Zcrit== -3.89
  }
  if (`cons'=="constant") & (`tr'="notrend") {
  scalar Zcrit== -3.86
  }
  if (`cons'=="constant") & (`tr'="trend") {
  scalar Zcrit== -3.39
  }
 drop r
 post Posting (`y') (`x') (Zobs) (Zcrit) ("`cons'") ("`tr'")
}

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