• Laura Whiting

The cond() Function – Evaluating True or False

The cond() function can be used to create a new variable based on true/false parameters. This function will evaluate expression(s) you give, and will record a value/string you specify if the expression is true, and it will record a different value/string you specify if the expression is false. You can also set a third value/string to be recorded if data is missing, although this is not required to run the function.

How to Use:

Worked Example:

For this example I use the auto dataset, which I load using the command sysuse auto, clear.

Its 1978 and I want to buy a car. I have $6000 to spend, but I want to make sure I’m getting a car that has a good repair record. From the list of cars in this dataset I am going to create a new variable to tell me which cars are both within my budget and have a reasonable repair rating. For the repair record, 1 is bad and 5 is the best. I want a car with a higher than average repair rating. In the command pane I type the following:

This lists the following to the Results pane:

As you can see there are quite a few cars with great repair records that come in under my budget.

In this example the cond() function created a new variable, putting “Yes” when all three of the expressions evaluated to true. If any of the expressions evaluated to false, then the function put a “No”. This command can be a useful tool when you need to evaluate a set of requirements as true or false.

11 views0 comments