My original data is like this with two variables: "class" and "classlist". I am trying to search in "class" and get the values that fulfills "econ 10@" (@ can be any number from 1-9) and add the values in the variable of "classlist".
class | classlist |
econ 101 | econ 511 |
econ 102 | econ 10@ |
econ 221 | |
econ 222 | |
econ 303 |
class | classlist |
econ 101 | econ 511 |
econ 102 | econ 10@ |
econ 221 | econ 101 |
econ 222 | econ 102 |
econ 303 |
Code:
levelsof class if regexm(class,"^econ 10"+"[0-9|A-Z]$"),local(list) foreach x of local list{ tab classlist replace classlist = "`x'" in `r(N)'+1 }
- use regular expression to find all the unique values in "class" that fulfill the format of "econ 10@"
- replace the (N+1)th observation of "classlist" with each of the unique values found above.
Code:
'2+1' invalid observation number
0 Response to Fail to feed local or scalar as the number of observation in a loop
Post a Comment