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: Re: Replace current values matching certain condition using values from other observations?


From   Chris Yang <[email protected]>
To   [email protected]
Subject   Re: st: Re: Replace current values matching certain condition using values from other observations?
Date   Sat, 25 Jan 2014 13:08:33 -0500

Brilliant. Thanks a lot!

On Sat, Jan 25, 2014 at 1:03 PM, Joseph Coveney <[email protected]> wrote:
> Chris Yang wrote:
>
> As a related question, if I have many variables that need to be
> replaced like this, is there a quick way to do them all at once
> instead of doing it one-by-one for each variable. Perhaps I could use
> a loop to loop through the variables, but if all the variable names
> follow a pattern, e.g. var*, is there a shortcut?
>
> I've tried: bysort group (seq): replace var*=var*[1] if seq > 1 for
> the example given, but it gives an error saying: "var ambiguous
> abbreviation".
>
> --------------------------------------------------------------------------------
>
> You'd use a -foreach- or -forvalues- loop over the variables.  Either
>
>     sort group seq
>     foreach var of varlist var* {
>         by group: replace `var' = `var'[1] if seq > 1
>     }
>
> or, if the variable names are distinguished by a numerical suffix, then
>
>     sort group seq
>     forvalues i = 1/3 {
>         by group: replace var`i' = var`i'[1] if seq > 1
>     }
>
> 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/
*
*   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