Home > Software engineering >  Generate new variable by grouping an old one according to its value characteristic
Generate new variable by grouping an old one according to its value characteristic

Time:02-11

I want to generate a new variable in Stata by grouping an old one according to its value characteristics. I have one variable called status that reflects employment type and status, so it has values like "Manager", "Ex-Manager", "Employee", "Ex-Employee".

Now I want to generate a new dummy variable that equals 1 if the person is still employed (contains "Manager" and "Employee") and equals 0 if the person is no longer employed ("Ex-Employee", "Ex-Manager").

CodePudding user response:

generate wanted = inlist(status, "Manager", "Employee") if !missing(status) 

will produce an indicator variable that is 1 if status is either of the specified values, 0 if it is another specified value, and missing if status is missing (meaning, an empty string).

See the help for inlist() and FAQs such as

https://www.stata.com/support/faqs/data-management/creating-dummy-variables/

https://www.stata.com/support/faqs/data-management/true-and-false/

Note that the comparison is entirely literal (no incorrect spellings or extra spaces or characters).

  • Related