1
votes

I am trying to check if the varlist in my program contains factor variables. I follow the recommendation by StataCorp in this page (http://www.stata.com/support/faqs/programming/factor-variable-support/). The suggested codes are:

syntax varlist(fv)
local fvops = "`s(fvops)'" == "true" | _caller() >= 11
if `fvops' {
    // within this loop, you can expand the factor variable list,
    // create a local for version control and perform any other
    // steps needed for factor operated varlists
}

Nevertheless, it now appears to me that the macro s(fvops) does not really check the existence of factor variables in the varlist. A simple example to illustrate:

capture program drop test_fvops
program test_fvops
syntax varlist(fv)
local fvops = "`s(fvops)'" == "true" | _caller() >= 11
qui reg `varlist'
if `fvops'  {
    di "factor variables specified"
}
else {
    di "factor variables not specified"
}
end

sysuse auto,clear
test_fvops price mpg foreign weight i.rep78
test_fvops price mpg foreign weight rep78

Oddly, the last two lines of the code both return "factor variables specified". The program could not tell the second line has no factor variables.

A further probe on the properties of the macro s(fvops) only makes me more confused.

sysuse auto,clear
qui reg price mpg foreign weight i.rep78
local fvops = "`s(fvops)'" != "true" | _caller() >= 11
di "|s(fvops)=`s(fvops)'|fvops=`fvops'|"

qui reg price mpg foreign weight rep78
local fvops = "`s(fvops)'" != "true" | _caller() >= 11
di "|s(fvops)=`s(fvops)'|fvops=`fvops'|"

In both cases, s(fvops) is empty but fvops is 1. I also tried to return list and sreturn list and did not see s(fvops).

Am I using the fvops code correctly? I am running this with Stata 13 on Win 7.

Added: Nick's comment clarifies something I couldn't find in the manual. So basically the _caller() segment shouldn't have been included, and this quote from the linked page was what I needed to understand about s(fvops).

"To check if factor variables were specified in the varlist, you may use the saved macro s(fvops) from syntax, which will be equal to “true” when factor variables are specified >and empty otherwise."

1

1 Answers

3
votes

With your own program: You are working with Stata 13 and your program does not set version to anything else.

Hence _caller() is greater than 11 in both cases and your test is no test.

Why not just try displaying the value of s(fvops)?

With the later code, your "further probe": Why expect s(fvops) to be visible? You never created it, as you did not use syntax. But the local macro fvops is 1 for the same reason: _caller() is more than 11 and the | operator selects 1.