Hello,
I am using STATA 15 and wide-format dataset.
My aim is to create a new variable (my data is confidential but as an example I will call it "newv") which I want to have a value of 1 or 0.
The value should be 1 if any of the values in a long list of other alphanumeric variables in the dataset contain a certain alphanumeric code.
As an example of the list of variables I will use "toronto" "paris" "rome" "madrid" here.
varname: toronto paris rome madrid
value: A1 b2 C3 D4
The list of variables in reality is very long and involves a lot of lines of code, so I am trying to use the foreach command.
The code I am using is :
gen newv=0
for each v of varlist toronto paris rome madrid {
replace newv=1 if strpos (v,"A1")>0
}
However, when I do this STATA says "{ required"
I thought I had put the braces in the correct place so I'm not sure where I am going wrong?
Any advice would be much appreciated,
Karyn
Related Posts with Using the foreach command
How to calculate the weakest link from a matrix?Dear Statalisters: I hope you are doing well. I am trying to calculate the weakest link from a sampl…
Multilevel model error with covariance (unstructured) - Hessian is not negative semidefinite conformability errorI'm testing a multilevel model with random slopes and a covariance structure for the error and reml …
Star option (for significance) for Pearson / Spearman correlation matrix (corsp)Dear Statalist members, I want to display the Pearson correlation coefficients and the Spearman ran…
Regressions with lagged structure using loop commandHi everyone, I have run the following regressions using the loop command which works just fine: C…
about margins for simple slopeI use STATA 14. When I use the margins to obtain simple slopes, it reports t values instead of z val…
Subscribe to:
Post Comments (Atom)
0 Response to Using the foreach command
Post a Comment