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 
 
Nick Cox <[email protected]> 
To 
 
"[email protected]" <[email protected]> 
Subject 
 
Re: st: replacing values problem 
Date 
 
Wed, 19 Mar 2014 20:59:34 +0000 
Your particular syntax problem I guess arises because -country- is a
string variable and your code treats it as numeric.
`parentlocation'
should be
"`parentlocation'"
Note that
replace country = country[_n-1] if missing(country)
is fine for your example, but the rest of your data might not be so
well behaved.
I am not a -merge-master, more a -by-pilot, but I'd see this as a -merge-.
See http://www.stata.com/statalist/archive/2014-03/msg00592.html for a
similar problem.
This code works, but the assumption is that no parents themselves have
parents in the dataset.
clear
input ID parentID str6 country
1 . "UK"
2 1 ""
3 1 ""
4 . "USA"
5 4 ""
6 4 ""
7 . "France"
8 7 ""
end
clonevar country2 = country
save mytest
keep if missing(parentID)
keep ID country2
rename ID parentID
list
merge 1:m parentID using mytest
replace country = country2 if missing(country)
list
Nick
[email protected]
On 19 March 2014 20:20, 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?
>
*
*   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/