Alexandre Loures
Alexandre Loures

Reputation: 211

Loop to replace values

I have 343 observations and am trying the following:

forvalues in 1/343 {
    replace `ido'="BRA" if `v2'=="F_3idoXidd_2_*"
    replace `ido'="AUS" if `v2'=="F_3idoXidd_3_*"
}

The * in F_3idoXidd_2_* is because I have 30 observations for each country, so I want to do all in one time.

Upvotes: 0

Views: 464

Answers (1)

ander2ed
ander2ed

Reputation: 1338

You generally don't need a loop to replace values of variables. In general, loops in Stata are not used to iterate over observations, but rather lists of some sort (varlist, numlist, etc.).

Also, your use of the wildcard won't function as you expect. Wildcards would also typically be used when specifying a varlist (drop var*, sum gdp*, etc.).

What you can do here instead is use strpos to search for the specified string in the variable, and replace its value conditional on the result of strpos. Example:

/* Create sample data */
clear *
input str15 v2
"F_03idoXidd_3_3"
"F_03idoXidd_2_3"
"F_03idoXidd_3_2"
"F_03idoXidd_2_2"
end
expand 50
gen ido = ""

replace ido = "AUS" if strpos(v2,"F_03idoXidd_3_")
replace ido = "BRA" if strpos(v2,"F_03idoXidd_2_")

or, a one line solution:

replace ido = cond(strpos(v2,"F_03idoXidd_3_"), "AUS", cond(strpos(v2,"F_03idoXidd_2_"),"BRA",""))

strpos returns 0 if the specified string is not found, and the position which it is first found otherwise. Used following the if qualifier, it is evaluated as true if > 0, and false otherwise. In this case, you could search v2 for F_3idoXidd_3_ and replace with AUS.

Of course this is just one approach and might not be ideal if you have many replacement values.

EDIT

Based on the comments to this answer, OP needs to create a second variable conditional on the value of the last integer in the string.

One method to do this relies on substr and assumes the F_3idoXidd_ portion of the string does not change across observations in such a way that different values (for example, F_4idoXidd_3_2) would have a different meaning than F_3idoXidd_3_2.

gen idd = ""
replace idd = "AUS" if substr(v2, -2,.) == "_3"
replace idd = "BRA" if substr(v2, -2,.) == "_2"

or again, a one line solution using substr and cond:

gen idd = cond(substr(v2,-2,.) == "_3", "AUS", cond(substr(v2,-2,.) == "_2", "BRA",""))

Again, this is only one way which springs to mind quickly. You may also be interested in looking at regexm and any number of the functions documented at help string_functions

Upvotes: 1

Related Questions