Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: Re: Accumulating variable names to exclude from a varlist

From   Partho Sarkar <>
Subject   st: Re: Accumulating variable names to exclude from a varlist
Date   Sun, 4 Mar 2012 14:50:04 +0530

Dear colleagues,

I have been struggling for an hour or so with this  problem, which is
probably trivial for those more familiar with Stata macros than I am,
so hope someone can offer a tip w/o too much trouble:

In a part of a program to read in and process data, I want to do the following:

Check a list of variables to see which of them exist in the file being
processed, build up a list of the ones that do exist, and then sort
the data by the existing variables.  I have tried to do this as

local varlist1 "date commodityid varietyid state district market"  //
In the file I am using to test, only 3 of these are present
local except
 foreach v in `varlist1' {
         capture confirm variable `v'
         if _rc==0 {
                 local except `except'
                               local except ` " `except'`v' " '   //
to accumulate the names of
the missing variables (spaces added here between quotes for clarity)
                               di "`except'"  // This works, as the
missing variables show up
cumulatively (w/o any spaces, though, in spite of the double quotes!)
                               local varlist2: list varlist1 - except
// trying to remove the
missing variable from the varlist-obviously this won't work as the
"except" macro resides within the above loop!
                               di "inner loop_""`varlist2'"


I am sure this could be done via global macros, but somehow I took it
as a challenge to do w/o those ( I am also not very sure how to
combine local & global macros)!  [ Is delayed macro substitution the
answer? If so, how?]

Sorry if this is too trivial!

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index