Hello Statlist,
I have an issue with my data that I have been trying to clean and would greatly appreciate your help on this.
I have a dataset as follows:
donor_country recipient_country year sector value
Austria Brazil 2002 Biodiversity 0.5
Austria Brazil 2002 Policy 0.2
Austria Mexico 2004 Biodiversity 0.1
Austria Mexico 2004 Research 0.3
Austria Colombia 2002 Preservation 0.2
Austria Colombia 2002 Education 0.5
I would like to create a new variable called biodiversity that is equal to value if the sector has biodiversity in it, but I want this value to repeat as long as the donor_country, recipient_country, & year don't change. So that it looks like this:
donor_country recipient_country year sector value biodiversity
Austria Brazil 2002 Biodiversity 0.5 0.5
Austria Brazil 2002 Policy 0.2 0.5
Austria Mexico 2004 Biodiversity 0.1 0.1
Austria Mexico 2004 Research 0.3 0.1
Austria Colombia 2002 Preservation 0.2 0
Austria Colombia 2002 Education 0.5 0
I tried using the following STATA command:
sort donor_country recipient_country year sector
egen biodiversity = total(value) if sector == "Biodiversity", by(donor_country recipient_country year)
bysort donor_country recipient_country year: replace biodiversity = biodiversity[1] if missing(biodiversity)
However I have noted that it works on some observations but not my all observations, Do you have any idea what I can do to fix it?
I have an issue with my data that I have been trying to clean and would greatly appreciate your help on this.
I have a dataset as follows:
donor_country recipient_country year sector value
Austria Brazil 2002 Biodiversity 0.5
Austria Brazil 2002 Policy 0.2
Austria Mexico 2004 Biodiversity 0.1
Austria Mexico 2004 Research 0.3
Austria Colombia 2002 Preservation 0.2
Austria Colombia 2002 Education 0.5
I would like to create a new variable called biodiversity that is equal to value if the sector has biodiversity in it, but I want this value to repeat as long as the donor_country, recipient_country, & year don't change. So that it looks like this:
donor_country recipient_country year sector value biodiversity
Austria Brazil 2002 Biodiversity 0.5 0.5
Austria Brazil 2002 Policy 0.2 0.5
Austria Mexico 2004 Biodiversity 0.1 0.1
Austria Mexico 2004 Research 0.3 0.1
Austria Colombia 2002 Preservation 0.2 0
Austria Colombia 2002 Education 0.5 0
I tried using the following STATA command:
sort donor_country recipient_country year sector
egen biodiversity = total(value) if sector == "Biodiversity", by(donor_country recipient_country year)
bysort donor_country recipient_country year: replace biodiversity = biodiversity[1] if missing(biodiversity)
However I have noted that it works on some observations but not my all observations, Do you have any idea what I can do to fix it?
Comment