Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: replacing a list of values within a loop


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: replacing a list of values within a loop
Date   Thu, 23 Feb 2006 22:13:38 -0000

I can't do miracles, just more Stata. You're focusing 
on -forvalues- when it's -foreach- that is more general. 

There are discursive tutorials at 

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros

SJ-2-2  pr0005  . . . . . .  Speaking Stata:  How to face lists with fortitude
        Q2/02   SJ 2(2):202--222                                 (no commands)
        demonstrates the usefulness of for, foreach, forvalues, and
        local macros for interactive (non programming) tasks

Suppose 

local BAD "STATA hate ignorance" 

local good "Stata love knowledge" 

so that there is a one-to-one mapping between the two. Then 

foreach d in G3M G3R G8M G8R {
	log using "C:/`d'_FixTeacher.txt", replace text
	use "C:/`d'.dta", clear

	forval i = 1/3 { 
		local b : word `i' of `bad' 
		local g : word `i' of `good' 
		replace teacherX = "`g'" if teacherX == "`b'"
	}

	save "C:/`d'_Clean.dta", replace 
}

If your IDs are numeric, the command is 

	replace teacherX = `g' if teacherX == `b' 

There are other ways of doing it. If your lists of BAD and good
are much longer, there might be better ways to do it. 

Nick 
[email protected] 

Gushta, Matthew
 
> i currently have 4 datasets containing teacher id's (var: teacherX).
> many teacher id's have been incorrectly entered and i would like to
> replace them with the correct values. for each dataset, i have
> identified 2 lists: 
> 1) list of BAD teacher id's;
> 2) list of GOOD teacher id's, to replace 1.
> 
> i am wondering if these lists of values can somehow be 
> incorporated in a
> "forvalues" loop? can i assign the values as a scalar? my anticipated
> coding is attached below. where "list of values" is written is where i
> hope a minor miracle will occur.
> 
> many thanks for any suggestions,

> /************************/
> local data1 "G3M"
> local data2 "G3R"
> local data3 "G8M"
> local data4 "G8R"
> 	// datafiles
> 
> local good1 "list of values"
> local good2 "list of values"
> local good3 "list of values"
> local good4 "list of values"
> 	// acceptable values
> 
> local bad1 "list of values"
> local bad2 "list of values"
> local bad3 "list of values"
> local bad4 "list of values"
> 	// rejected values
> 
> forvalues i = 1/4 {
>   log using "C:/`data`i''_FixTeacher.txt", replace text
>   use "C:/`data`i''.dta", clear
>   replace teacherX==`good`i'' if (teacherX==`bad`i'')
> 	// replace bad values with good values
>   save "C:/`data`i''_Clean.dta", replace 
> }
> /************************/
> 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index