Bookmark and Share

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


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

Re: st: Storing error messages as local macros


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Storing error messages as local macros
Date   Wed, 27 Nov 2013 01:10:27 +0000

Sarah's helpful advice reminds me that I once wrote a helper program
-isvar-, which is on SSC.

-------------------------------------------------------------------------------------------------------
help for isvar
-------------------------------------------------------------------------------------------------------

Filter names into variable names and others

        isvar possiblevarlist


Description

    isvar takes a list of names that might name variables in your
dataset and filters it into a list
    of those names that are indeed variable names and a list of the others.

    One application of isvar arises when you are moving between
similar datasets, especially if they
    are large, but are not sure which variables are defined in which
dataset. Commands such as
    describe would fail at the first name not in fact a variable name.
isvar offers a more direct way
    to establish existence or non-existence of several possible variables.


Saved results

    r(varlist) names of variables in current dataset
    r(badlist) names that do not correspond to variables in current dataset


Examples

    . isvar mpg rep78 rep77
    . local OKlist "`r(varlist)'"
    . su `OKlist'


Author

    Nicholas J. Cox, Durham University, U.K.
    [email protected]


Acknowledgements

    This problem was suggested by Amadou Diallo.


Also see

    Online:  help for describe; unab

Nick
[email protected]


On 27 November 2013 00:48, Sarah Edgington <[email protected]> wrote:
> Kenneth,
> I've used -desc, varlist- for similar tasks where I have multiple files with
> different permutations of the variables.
> One way to do this is something like:
>
> desc, varlist
> **create a macro with a space before and after the varlist so you can always
> match on space[varname]space
> **otherwise you might have to worry about vars that match substrings of
> other vars
> local indata " `r(varlist)' "
> **you're probably embedding this in another loop so reset the local keeplist
> in case it isn't empty
> local keeplist ""
> foreach keepvar in schoolid distid math read language  {
>         **for each variable in your list, cycle through and check that it's
> in the variable list.
>         **if it is, add it to a keeplist macro
>         if strmatch("`indata'","* `keepvar' *") local keeplist "`keeplist'
> `keepvar'"
> }
> keep `keeplist'
>
>
> There are probably more elegant solutions but this does get around the issue
> of having to parse the error messages.
>
> -Sarah
>
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Nick Cox
> Sent: Tuesday, November 26, 2013 4:15 PM
> To: [email protected]
> Subject: Re: st: Storing error messages as local macros
>
> Interesting question. I don't know a way to store the error message.
> What you can do is store the code and replay the message at any later time.
> For example -error 198- replays the message for 198, regardless of whether
> was the last error.
>
> The commands
>
> local rc = _rc
> if `rc' != 0 <whatever>
>
> can be compressed to
>
> if _rc != 0 <whatever>
>
> and indeed to
>
> if _rc <whatever>
>
> but it remains true that you would need to store non-zero error codes for
> later use as _rc contains only a code for the last command issued.
> Nick
> [email protected]
>
>
> On 26 November 2013 23:39, Kenneth Shores <[email protected]>
> wrote:
>> Hi stata list-serve:
>>
>> Here is a question I have not seen asked. I am importing a large
>> number of datasets, keeping variables that I want, doing a number of
>> reshapes and saving the dataset.
>>
>> In each of the datasets, there are core variables that are common
>> across all datasets. However, there are additional variables that I
>> want that are not common to all datasets. For example, in dataset i,
>> there is a school identifier, district identifer, reading, language
>> and math scores. In dataset j, there is a school identifier, district
>> identifier, reading, and math scores.
>>
>> The first command I wish to run is "keep schoolid distid read math
> language"
>> This will return an error when I try to bring up dataset j.
>>
>> The alternative I use is:
>> cap keep schoolid distid read math language loc rc = _rc if `rc' != 0
>> keep schoolid distid read math
>>
>> This works, insofar as all datasets look like i and j. However,
>> dataset k has schoolid, read, and math. Now the new keep command
>> breaks.
>>
>> There are many different permutations of this. Moreover, there are 300
>> datasets and programming a specific keep command for each one will be
>> time consuming  (there are also more than 5 variables that I want).
>>
>> It seems that I could make use of the error message itself in these
>> cases. Because the error message informs me about what vars are not
>> found, I could store those not found vars in a local macro. The final
>> code would then be something like this:
>>
>> local keepvars "schoolid distid math read language"
>> cap keep `keepvars'
>> [store error message, parse "vars not found portion"] loc rc = _rc if
>> `rc'!=0 loc keepvarsalt: subinstr local keepvars "`[parsed error
>> message]'" ""
>> keep `keepvarsalt'
>>
>> this would be very flexible and would make use of the specificity of
>> the Stata error message. However, I cannot find any way to store the
>> error message text. Is it not possible? And if not, are there
>> solutions to this problem that others are aware of.
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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