If you had many more values, you could loop over the distinct values sought, using levelsof if they are in a second variable, or put the candidates in a separate dataset and merge as explained in this FAQ.Īll these techniques depend on exact equality of strings, so watch out for variations between upper and lower case, leading and trailing spaces and inconsistencies in spelling. Sometimes when you import a new dataset or create a new dataset you realise that the variable names are not appropriate for the work you are doing. Gen wanted = inlist(list_a, "cricket", "hockey", "swimming") replace changes the contents of a variable. But replace is needed not infrequently if you cannot or do not want to create a new variable in a single rush. generate newvar exp creates the new variable from existing variables through an expression. by state: gen lag1 x n-1 If there are gaps in your records and you only want to lag successive years, you. The five commands below are often used to create or modify variables. This is a good example of one of Statas built-in. gen lead1 x n+1 You can create lag (or lead) variables for different subgroups using the by prefix. se1+se2+se3+se4)/4 This would create a new variable called se that had as its value the average of. There is a simple technique that works fine for your toy example: clear Create lag (or lead) variables using subscripts. If you have certain typos that you additionally want to take into account, you can combine my solution with In the above loop use instead: replace Cont = 1 if inlist(list_a, "hoccckey") | list_a = list_b in `i' Replace Cont = 1 if list_a = list_b in `i' The following illustrates the philosophy: local obs = _N
Instead, you need to compare the value at each row of list_a, with all values of variable list_b. The answer is no because your approach will compare the values of the two variables in each observation.