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: Re: -label define- and -replace- when a variable may be missing


From   "Joseph Coveney" <[email protected]>
To   <[email protected]>
Subject   st: Re: -label define- and -replace- when a variable may be missing
Date   Sun, 9 Mar 2014 15:25:45 +0900

Michael McCulloch wrote:

I am cleaning data for a survey, in which for one question, respondents were
asked where they got their training.
There are 9 possible answers for this question, but as I monitor survey data
coming in, some may have missing values.
The online survey instrument creates the variables as they are filled in:
	what_types_of_training_did___1
	what_types_of_training_did___2, and so on up to 
	what_types_of_training_did___9	

In order to create reports but not have my do-file stopped by a missing (not yet
defined) variable, how can I modify the following code?

	gen training=. 
	replace training=1 if what_types_of_training_did___1==1 
	replace training=2 if what_types_of_training_did___2==1 
	replace training=3 if what_types_of_training_did___3==1 

	lab def lab_train 	1 "Accredited US School" ///
					2 "Accredited School outside US" ///
					3 "Unaccredited US School", modify

--------------------------------------------------------------------------------

I'm not quite sure what you've got in your dataset(s), but couldn't you test 
for existence of a variable in each passage of a loop?  Something like 

generate byte training = 0
foreach var of varlist what_types_of_training_did___? {
    local index : subinstr local var "what_types_of_training_did___" ""
    // Alternative:  local index = substr("`var'", -1, 1)
    quietly replace training = `index' if `var' == 1
}
#delimit ;
label define TrainingTypes
  1 "Accredited US School"
  2 "Accredited School outside US"
  3 "Unaccredited US School"
  . . .
  9 "Hard Knocks";
#delimit cr
label values training TrainingTypes

Depending upon the structure of your dataset(s), you might be able to use
-reshape- and -rename-, skipping the loop.

Joseph Coveney

*
*   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