Suppose I have data that looks like the following:
I would like to save a local macro that is equal to 1 if the variable x contains the string "COST" in any row, and 0 if it does not contain it anywhere. To do this, I could do the following:
Does anyone know of a command that does this more efficiently? Thanks!
Code:
clear set obs 5 gen x = "" replace x = "COST YTD" in 3
Code:
gen x_cost = strpos(x, "COST")
egen x_cost_max = max(x_cost)
if x_cost_max != 0 {
local cost_str_exists = 1
}
else {
local cost_str_exists = 0
}

Comment