Hello,
I am operating Stata/SE 15.1 on a Windows Server.
I have a string variable, called object_description, and I want to create another string variable, called product_id, which will be equal to "XYZ" if object_description contains the character "SHIP".
To complicate the problem, variable object_description also contains observations with words such as: "LIGHTSHIP", "MEMBERSHIP" and "SHIPPING". I don't want my new variable product_id to be equal to "XYZ" if object_description contains such expressions. Therefore, I do the following:
I also tried:
Neither work.
In fact, after I run the code and I browse the dataset, I find that whenever object_description contains, say, "MEMBERSHIP", my new variable product_id is equal to "XYZ", as if Stata were not reading what follows the &-statement.
I am very confused. Any suggestion would be much appreciated.
Cordially
Edoardo
I am operating Stata/SE 15.1 on a Windows Server.
I have a string variable, called object_description, and I want to create another string variable, called product_id, which will be equal to "XYZ" if object_description contains the character "SHIP".
To complicate the problem, variable object_description also contains observations with words such as: "LIGHTSHIP", "MEMBERSHIP" and "SHIPPING". I don't want my new variable product_id to be equal to "XYZ" if object_description contains such expressions. Therefore, I do the following:
Code:
gen product_id = "XYZ" if strpos(object_description, "SHIP") & ( !(strpos(object_description, "MEMBER")) | !(strpos(object_description, "LIGHT")) | !(strpos(object_description, "PING")) )
Code:
gen product_id = "XYZ" if strpos(object_description, "SHIP") & ( !(strpos(object_description, "MEMBERSHIP")) | !(strpos(object_description, "LIGHTSHIP")) | !(strpos(object_description, "SHIPPING")) )
In fact, after I run the code and I browse the dataset, I find that whenever object_description contains, say, "MEMBERSHIP", my new variable product_id is equal to "XYZ", as if Stata were not reading what follows the &-statement.
I am very confused. Any suggestion would be much appreciated.
Cordially
Edoardo
Comment