Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

RE: st: conditional "keep" statement

From   "Nick Cox" <>
To   <>
Subject   RE: st: conditional "keep" statement
Date   Mon, 12 Apr 2010 19:18:48 +0100

A couple of belated footnotes: 

1. -isvar- from SSC is focused on filtering name lists into varlist and not. 

2. -help undocumented- reveals a panoply of filtering commands, although on a quick glance not one that solves this problem. 


David Souther

.Many thanks to Eric Booth, Robert Picard, + David Kantor for the
help.  I found that Robert's approach worked for me given my large
num. of variables.   

On Mon, Apr 12, 2010 at 10:07 AM, David Kantor <> wrote:
> At 09:54 AM 4/12/2010, David Souther wrote:
>> I'm combining (append & merge) hundreds of files which contain
>> hundreds or thousands of variables each, and at each stage of the
>> merge/append, I'd like to variables only if they are in a list that I
>> specify.
>> The issue is that not all of these datasets contain all the variables
>> in this list.  So, if I create a macro of my list,
>> local myvars  a1 a2 a3 d1 d2 d3
>> and then specify,
>> keep `myvars'
>> in the loop for each combination of data, I get the error that some
>> variable doesn't exist when it's not in the dataset.  I tried getting
>> around this with the -capture- command,
>> cap keep `myvars'
>> and using -ds-
>> ds `myvars'
>> keep `r(varlist)'
>> or,
>> ds `myvars', not
>> drop `r(varlist)'
>> and -ds- in combination with -capture- to no avail.   Any other ideas
>> (besides specifying the hundreds of variables to keep for each/every
>> loop)??
>> [...]
> I'm not sure what to do if you bump into limits of macros, but I wonder if
> you know about macrolists. This may be useful.
> local myvars  a1 a2 a3 d1 d2 d3
> ds
> localdatasetvars "`r(varlist)'"
> local commonvars : list myvars & localdatasetvars
> commonvars now contains the variable names common to the dataset and to
> myvars.
> See -help macrolists-.

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index