Statalist


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

st: looping over local macro


From   "Eric A. Booth" <[email protected]>
To   [email protected]
Subject   st: looping over local macro
Date   Fri, 1 May 2009 08:53:45 -0500

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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index