Statalist


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

st: Re: looping over local macro


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Re: looping over local macro
Date   Fri, 1 May 2009 16:04:11 +0200

<>

***
clear

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

gen v2=inlist(v1, 55555, 66666, 77777, 77778, 88888, 99999)
gen v3 =inlist(v1,  55555, 66666, 77777, 77778, 88888, 99999)

list, noobs
***

HTH
Martin _______________________ ----- Original Message ----- From: "Eric A. Booth" <ebooth@ppri.tamu.edu>
To: <statalist@hsphsun2.harvard.edu>
Sent: Friday, May 01, 2009 3:53 PM
Subject: st: looping over local macro


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