Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: New command in SSC: -checkfor2-

From   "Nick Cox" <>
To   <>
Subject   RE: st: RE: New command in SSC: -checkfor2-
Date   Mon, 19 Sep 2005 23:38:07 +0100

There is no clash here. Let's back track: 

Amadou Diallo wants to be able to type a list 
of (possible) variable names and be told which 
are real variable names and which aren't. 

If Amadou were to try, for example,  

. sysuse auto 

. d mpg disp frog toad newt darwin einstein newton price 

Then -describe- just chokes on the first non-variable 
name, in this case "frog". -describe- won't tell 
Amadou that "toad newt darwin einstein newton" 
are also non-variable names. Same is true for e.g. 
-ds-. In my own view this is a feature, as I have never 
wanted what Amadou wants here, but the issue is how 
to write a program to deliver on that. 

Now in designing a program to get round this, 
Amadou faces what looks like Catch-22. 

syntax varlist 

is out of the question, because those non-variable
names will never get past border control. At the 
first such, it's all up. So any program starting with 
-syntax varlist- will not work, insofar as the bad 
names other than the first just will not be displayed. 

Amadou worked that out, and chose 

syntax anything 

But it looks at first sight as if thereby the programmer
is giving up on nice features that many users exploit 
all the time with varlists, such as wildcards and hyphenated lists. 

But with -syntax anything- you can have, well, anything
as input. You just have to arrange to process it according to 
your precise desires. So that -anything- can be, or 
can include a varlist, or several varlists interspersed
with other stuff: it's just that -syntax- won't treat it as such. 

Enough of the arm-waving. This code sketch shows 
one solution to that desire. I loop over the input 
with -foreach-, much faster than a -while- and -mac shift-. 

*! NJC 1.0.0 19 Sept 2005 
program isvar, rclass  
	version 8 
	syntax anything 
	foreach v of local anything { 
		capture unab V : `v' 
		if _rc == 0 local varlist `varlist' `V' 
		else        local badlist `badlist' `v'  


	if "`varlist'" != "" { 
		local n : word count `varlist' 
		local what = plural(`n', "variable") 
		di as txt "{p}`what': `varlist'{p_end}" 
		return local varlist "`varlist'" 
	if "`badlist'" != "" {
		local n : word count `badlist' 
		local what = plural(`n', "not variable") 
		di as txt "{p}`what': `badlist'{p_end}" 
		return local badlist "`badlist'" 

This isn't a substitute for -checkfor2-. -checkfor2- 
bundles three things together, a check for good
and bad variable names, a report on number of 
missings and summary statistics for any good
variable names, so the last two things aren't touched. 

I couldn't find a -checkfor-, not that that matters. 

Otherwise I agree with all Jean-Benoit's comments. 

I know the World Bank would like us all to think 
globally, but rclass in my view too is the way 
to go here. 


Jean-Benoit Hardouin

> I find several drawbacks in the programing of -checkfor2- :
>       - the use of "anything" instead of "varlist" in the 
> syntax command 
> avoids to use list of variables as var1-var10 or var*
>       - the idea to use a global macro variable is, from my 
> mind, a bad 
> idea (I prefer a "return" command)

*   For searches and help try:

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