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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: conditional "keep" statement


From   David Souther <davidsoutheremail@GMAIL.COM>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: conditional "keep" statement
Date   Mon, 12 Apr 2010 09:45:23 -0500

>
Yeah, this works on the smaller datasets , but the macro size limit
does make this unusable when the number of variables is in the
thousands.    I tried messing with the macro functions to break the
macro "mine" into pieces, but cannot get it to work (or whether that
is really the intention of the "pieces #1 #2" function).

If the macro function idea is a bust, anyone else have some other ideas?

D.S.



On Mon, Apr 12, 2010 at 9:16 AM, Eric Booth <ebooth@ppri.tamu.edu> wrote:
>>
>
> You could use the extended macro function "subinstr" to filter the list in a loop (though I wonder if macro length limit will bite if you've got of hundreds or thousands of variable names):
>
> *****
>> local myvars  a1 a2 a3 d1
>
> ds
> global mine `r(varlist)'
>                di "$mine"
> foreach v in  `myvars' {
>        di "`v'"
>        global mine: subinstr global mine "`v'" "",  word all
>        }
>                di "$mine"
> drop "$mine"
> *****
> This should drop only those in the list that aren't in your `myvars' list.
>
> ~ Eric
> __
> Eric A. Booth
> Public Policy Research Institute
> Texas A&M University
> ebooth@ppri.tamu.edu
> Office: +979.845.6754
>
>
>
>
> On Apr 12, 2010, at 8:54 AM, 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)??
>>
>> D.S.
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
>
>
>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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