Hello,
I was hoping to have some help with the Stata command ‘cond’ used to create sequences from a variable in a dataset.
I am using data from a dual-response Discrete Choice Experiment. That is, there were four job options that a respondent could face in the same job choice: Job A vs Job B, and then Previous Choice vs Current Job.
In the dataset, the variables take the following numerical values:
Ideally, for each respondent (id) who chose a job (q_) in a set of options (choiceset), the alternative job (q_alt) should be displayed in the dataset. To obtain this, I used the following Stata command:
# by id choiceset: replace q_alt=cond(q==1, 2, 1) if _n==2
I then merged the two datasets with the variables ‘chosen, q_ and q_alt’ and the one with the DCE variables (resources, relationship, etc).
In essence, this is what I should obtain the following:
Instead, I obtain the following:
I tried different combinations of the above commands, but to no avail. Could I please have some guidance regarding the ‘cond’ command, as I suspect the main issue comes from there? Many thanks for your help.
Zoé
I was hoping to have some help with the Stata command ‘cond’ used to create sequences from a variable in a dataset.
I am using data from a dual-response Discrete Choice Experiment. That is, there were four job options that a respondent could face in the same job choice: Job A vs Job B, and then Previous Choice vs Current Job.
In the dataset, the variables take the following numerical values:
- Job A=1
- Job B=2
- Previous Choice=3
- Current Job=4
Ideally, for each respondent (id) who chose a job (q_) in a set of options (choiceset), the alternative job (q_alt) should be displayed in the dataset. To obtain this, I used the following Stata command:
# by id choiceset: replace q_alt=cond(q==1, 2, 1) if _n==2
I then merged the two datasets with the variables ‘chosen, q_ and q_alt’ and the one with the DCE variables (resources, relationship, etc).
In essence, this is what I should obtain the following:
| chosen | q_ | q_alt | resource_dce | relationship |
| 1 | Job B | Job B | 2 | 2 |
| 0 | Job B | Job A | 0 | 0 |
| 1 | My previous choice | My previous choice | . | . |
| 0 | My previous choice | My current choice | . | . |
Instead, I obtain the following:
| chosen | q_ | q_alt | resource_dce | relationship |
| 1 | Job B | Job B | 2 | 2 |
| 0 | Job B | Job A | 0 | 0 |
| 1 | My previous choice | My previous choice | . | . |
| 0 | My previous choice | Job A | . | . |
I tried different combinations of the above commands, but to no avail. Could I please have some guidance regarding the ‘cond’ command, as I suspect the main issue comes from there? Many thanks for your help.
Zoé

Comment