|
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: Re: Seeking explanation of the internal procedures of -replace-
From
"Michael Blasnik" <[email protected]>
To
<[email protected]>
Subject
st: Re: Seeking explanation of the internal procedures of -replace-
Date
Thu, 24 May 2007 07:27:08 -0400
...
Stata works sequentially through the variable in a replace, which allows
functions such as sum() to accumulate a running sum and allows [_n-1] constructs
to work properly. I don't see why you need to use clonevar and then rename and
drop -- you could just use a temporary variable to hold the original values and
then replace the originals with the tempvar at the end. I wrote an ado to do
what you want except I didn't include an [if] option which you could probably
add without too much work. It's short enough to include here:
program define scramble
version 9.2
syntax [varlist]
tempvar hold order rand
gen long `order'=_n
foreach var of local varlist {
gen `rand'=uniform()
gen `hold'=`var'
sort `rand'
replace `var'=`hold'[`order']
drop `rand' `hold'
}
end
You could hard code your -if- condition by changing the last -replace- line
with:
replace `var'=`hold'[`order'] if indexnot(`var',"?") != 0
or you could add a couple of more lines to make the program accept an -if-
Michael Blasnik
----- Original Message -----
From: "Jesper Kj�r Hansen" <[email protected]>
To: <[email protected]>
Sent: Wednesday, May 23, 2007 11:31 PM
Subject: st: Seeking explanation of the internal procedures of -replace-
<snip>
I am working on a simulation procedure, which randomly permutes
all variables in a varlist before each run (optionally permutes
groups of variables together).
Given original variable dtsim20, I would have thought that the
the two following command sequences would yield the same result,
because my understanding of -replace- was that it works on a
temp copy of the variable, which will not be replaced before
the very end of the -replace- procedure.
[see the full, but very basic example at the end of this post]
1. The "correct" command sequence(?)
. clonevar _dtsim20 = dtsim20
. replace _dtsim20 = dtsim20[dtrank] if tag
2. Incorrect, because of [some sort of circular ref.]?
. clonevar wrong = dtsim20
. replace wrong = wrong[dtrank] if tag
Question:
Could someone please explain exactly why (2) doesn't work?
I guess that I will go with (1), unless someone has a more
clever idea? The only reason I'm asking is that I now have
to do quite a lot of clone/replace/drop/rename in my procedure.
Thanks in advance.
/Jesper K. Hansen
*
* 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 |