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   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Tracking Replacements Made to Variables
Date   Tue, 1 Feb 2011 08:44:08 +0000

Eric gave you precisely what you asked for, but Daniel's solution is
much more efficient and equivalent. Note the spelling of Stata,
corrected below.

Nick

On Tue, Feb 1, 2011 at 3:20 AM, Eric Booth <[email protected]> wrote:
>
> *************!
> 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**
>
> On Jan 31, 2011, at 3:13 PM, Christopher Lanoue wrote:
>
>> 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."

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