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: Tracking Replacements Made to Variables


From   Eric Booth <[email protected]>
To   "<[email protected]>" <[email protected]>
Subject   Re: st: Tracking Replacements Made to Variables
Date   Tue, 1 Feb 2011 03:20:08 +0000

<>

*************!
clear
inp yr        var1
1961    15
1962     .
1963    20
1964     .
1965    30
end

g var2 = var1  

forval n = 1/`=_N' {
**tracking**
	**display**
if mi(var1[`n']) di in y "var 1, year `=yr[`n']'. Avg of `=yr[`n'-1]' and `=yr[`n'-1]'."
	**stored in -char-s for later retrieval**
if mi(var1[`n']) char var1[obs`n'] "var 1, year `=yr[`n']'. Avg of `=yr[`n'-1]' and `=yr[`n'-1]'."
**replace**
replace var2 = (var1[_n-1]+var1[_n+1])/2 if mi(var1)
  }

  char li        //list char for changes obs


**also, take a look at -impute-, e.g.
impute var1 yr, g(var3)
*************!

**watch for wrapping issues in code above**

- Eric
__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754

On Jan 31, 2011, at 3:13 PM, Christopher Lanoue wrote:

> Hi,
> My problem is that I am trying to keep track of changes that I make to
> my data using STATA. For instance if I have data such as:
> 
> yr        var1
> 1961    15
> 1962     .
> 1963    20
> 1964     .
> 1965    30
> 
> and I want to take the average of the two data points to fill in the
> missing values.
> 
> I run replace var1 = (var1[_n-1]+var1[_n+1])/2 if var1 == . to
> accomplish this task, but STATA simply outputs (2 changes made) to the
> display. Is there any way to capture the changes STATA makes, i.e.
> loop through the changes (if the changes are saved somewhere) and
> output something like "var 1, year 1962. Average of 1961 and 1963."?
> If this is not possible, is there are a way to use subscripts on the
> variable so that I can loop through each value and if a change is
> made, output the string above, i.e. replace var1[`i'] = .... and then
> display "var 1, yr[`i']. Average of yr[`i'-1] and yr[`i']+1."
> Any help would be greatly appreciated.
> Thanks in advance,
> Chris
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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