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]

st: Storing error messages as local macros


From   Kenneth Shores <[email protected]>
To   [email protected]
Subject   st: Storing error messages as local macros
Date   Tue, 26 Nov 2013 15:39:16 -0800

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/


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