Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: replacing values problem
From
Sergiy Radyakin <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: replacing values problem
Date
Wed, 19 Mar 2014 17:00:00 -0400
clear all
input ID parentID countryID
1 . 1
2 1 .
3 1 .
4 . 2
5 4 .
6 4 .
7 . 3
8 7 .
end
list
preserve
keep if parentID==.
tempfile parents
drop parentID
rename ID parentID
sort parentID
save "`parents'"
restore
sort parentID
rename countryID cID
merge parentID using "`parents'", keep(countryID) update
drop _merge
gen country=min(cID, countryID)
drop cID countryID
sort ID
list
-----------------------------------------------
ID parentID country
1. 1 . 1
2. 2 1 1
3. 3 1 1
4. 4 . 2
5. 5 4 2
6. 6 4 2
7. 7 . 3
8. 8 7 3
On Wed, Mar 19, 2014 at 4:20 PM, Paula Smith <[email protected]> wrote:
> Hi all,
>
> I have a database of parents and children with variables indicating parent =
> ID and country of parent:
>
> ID parentID country
> 1 . UK
> 2 1
> 3 1
> 4 . USA
> 5 4
> 6 4
> 7 . France
> 8 7
>
> I'm wanting to put give the children the same value of 'country' as the par=
> ent. The 'country' variable is a string.
>
> So far I've got:
>
> quietly levelsof country if country!="", local(parentlocation)
> quietly levelsof parentID if parentID!=. , local(parent)
> quietly levelsof ID if parentID!=. , local(child)
> foreach l of local child {
> quietly replace country=`parentlocation' if ParentID==`parent'
> }
>
> But I'm getting an r(198) error. Am I over-complicating things? Can anyone =
> help?
>
> Thanks in advance,
> Paula
>
>
>
>
>
>
>
>
>
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/