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

st: RE: Local in -if expressions

From   "Nick Cox" <>
To   <>
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 

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 

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

In fact there is an extra bug in this line: it just has not bitten you
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. 


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

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)


Paulo Regis

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:

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