Nick
thanks for your help. I'm running your second option for irregular suffixes
and, although I don't seem to be getting error messages, it isn't working.
The first vector comprises variables:
rpy204 rpy203 rpy199 rpy206 rpy210 rpy202 rpy014 rpy207 rpy209 rpy013 rpy201
rpy200 rpy018 rpy016 rpy017
the second vector comprises:
_rpy204 _rpy203 _rpy199 _rpy206_rpy210 _rpy202 _rpy014 _rpy207 _rpy209
_rpy013 _rpy201 _rpy200 _rpy018 _ rpy016 _rpy017
I have written the following code which produces the output as shown (note
that I'm also including an if command as I only want to copy over values
conditional on the value of additional variable). The values of the
variables in the first vector (or set) remain uncyhanged when I tab them.
Any ideas?
Patrick
. foreach v of var rpy* {
2. local new : subinstr local v "rpy" "_rpy"
3. replace `new' = `v'
4. } if show3 == 1
(8334 real changes made)
(8328 real changes made)
(8333 real changes made)
(8334 real changes made)
(8347 real changes made)
(8348 real changes made)
(8333 real changes made)
(8410 real changes made)
(8335 real changes made)
(8334 real changes made)
(8350 real changes made)
(8337 real changes made)
(8328 real changes made)
(8390 real changes made)
(8417 real changes made)
-----Original Message-----
From: [email protected]
[mailto:[email protected]]On Behalf Of Nick Cox
Sent: 28 May 2003 10:56
To: statalist
Subject: st: RE: looping through vectors
Patrick Sturgis
> I have two vectors of variables, X and Z. I wish to copy
> the values of each
> of the elements of Z to the corresponding (in terms of
> variable order)
> element in X. Would the 'foreach' command be most suitable
> for this purpose
> or would something simpler suffice? Thanks in advance,
I guess that a vector of variables Z is a set of variables.
In the easiest case you have some simple naming like
Z1 ... Z42.
To copy that to X1 ... X42 you can go
forval i = 1/42 {
gen Z`i' = X`i'
}
(for -gen- read -replace- if that is what you want).
For a messier case with irregular suffixes like
Zbill, Zvince, Zalan, Zpat, Zbobby, Zchinh, Zken, etc.
for which you want the corresponding
Xbill, Xvince, Xalan, ...
one way of doing it is
foreach v of var Z* {
local new : subinstr local v "Z" "X"
gen `new' = `v'
}
See -whelp macro-.
Sometimes with just a few suffixes it can be
easiest just to go
foreach s in bill vince alan {
gen X`s' = Z`s'
}
Nick
[email protected]
*
* 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/
*
* 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/