Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: looping over local macro


From   "Eric A. Booth" <[email protected]>
To   [email protected]
Subject   Re: st: RE: looping over local macro
Date   Fri, 1 May 2009 09:16:20 -0500

Thanks. Both Nick and Martin's approaches (removing the extra - replace- in the second loop & using -inlist()- ) worked perfectly.



On May 1, 2009, at 9:06 AM, Nick Cox wrote:

Apologies for previous premature post.

Your punishment is that you got what you asked for!

First time round the loop, you recoded missings in -v2- to 1 if -v1- was
55555 and 0 otherwise.

As this divides up the field exhaustively, that first step means that
there are no longer any missings, so no further changes take place.

Your line

	recode v3 (.= 0) if `val' != v1

should be removed or moved.

Nick
[email protected]

Eric A. Booth

I am trying to recode a variable (v2 or v3) based on whether the
observation in v1 is present in a list of numbers (a local macro).
Below I've pasted code for my two different approaches, both using -
macro shift-, but neither approach gets past the first item in the
list "55555".


**********
clear

input v1
55555
22222
11111
66666
11111
88888
77778
99999
33333
end

gen v2 = .
tokenize  55555 66666 77777 77778 88888 99999
while "`1'" != ""  {
	recode v2 (.= 1) if `1' == v1
	recode v2 (.= 0) if `1' != v1
	macro shift
    }
	
gen v3 = .
local zip  55555 66666 77777 77778 88888 99999
foreach val of local zip  {
	recode v3 (.= 1) if `val' == v1
	recode v3 (.= 0) if `val' != v1
	macro shift
    }

		
*********

The resulting dataset looks like:
v1		v2	v3
55555	1	1
22222	0	0
11111	0	0
66666	0	0
11111	0	0
88888	0	0
77778	0	0
99999	0	0
33333	0	0

Thanks for any help!

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index