Hello everyone,
I have a dataset with airline carriers. I want to write an if command that runs if the name of the airline carrier (string variable) on the row is the same as the name of the airline carrier six rows above it. After much trying, I have come to realize that you cannot do an if command to compare strings (at least I do not think it can be done). However, I believe you can generate an indicator variable to see if the names are the same, and write an if command based on that indicator variable. However, when I do this, the if command does not execute when it is supposed to. Below is my code with more explanation after.
The first line creates my variable stderr which may be changed by the if statement. I then create name1 and name2 to make sure when browsing the data that I am looking at the correct names. Then "gen firmcheck" outputs a zero if the names are the same (it does this correctly when I browse the data). However, the if command never runs, even if the firmcheck variable is 0 when I browse. I understand this function may be easier to do with a replace stderr = 0 if [expression] however I have simplified the if commands within the brackets, it will do a lot more. I just need to get the if command to do what it is supposed to.
Thanks for all of the help,
-David
I have a dataset with airline carriers. I want to write an if command that runs if the name of the airline carrier (string variable) on the row is the same as the name of the airline carrier six rows above it. After much trying, I have come to realize that you cannot do an if command to compare strings (at least I do not think it can be done). However, I believe you can generate an indicator variable to see if the names are the same, and write an if command based on that indicator variable. However, when I do this, the if command does not execute when it is supposed to. Below is my code with more explanation after.
Code:
gen stderr = 0 gen name1 = unique_carrier_name gen name2 = unique_carrier_name[_n-6] gen firmcheck = name1 != name2 if firmcheck == 0 { replace stderr = 1 }
Thanks for all of the help,
-David
Comment