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 00:14:49 +0000
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.
>
> Thanks in advance,
> -Kenneth Shores
> PhD Candidate, Stanford Univ. School of Education
> *
> * 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/
*
* 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/