Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help with 'cond' command to create alternative job choices and then merge Discrete Choice Experiment datasets

    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:
    • Job A=1
    • Job B=2
    • Previous Choice=3
    • Current Job=4
    I was able to change the dataset from wide to long format, created a dummy variable (chosen) taking the value one when the job choice (q_) was chosen and zero otherwise, and expanded the dataset to ensure each respondent has four observations.

    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é

  • #2
    cond() is a function, not a command. In Stata the terms are not synonyms.

    More crucially, I can't yet see confidently what the problem is without a data example, but my guess is different. The problem lies in a nuance of use of by:

    Did you want

    Code:
     by id (choiceset): replace q_alt=cond(q==1, 2, 1) if _n==2

    Comment


    • #3
      Dear Nick
      Thanks for getting back to me and for clarifying. Sorry for the typo.

      Here's a screenshot of what the DCE data looks like after expanding it.
      • The q_alt variable shows the chosen(0/1) Job choice (in duplicate format as the dataset has been expanded)
      Here is the main problem:
      • The q_dup variable should show the alternative Job choice; i.e. when Job B is chosen in row 1 (q_alt), q_dup should show Job A in row 2
      • Similarly, when 'My previous choice' is chosen in row 3 (q_alt), in row 4 'My current job' should be displayed.
      • I should have obtained this result using the cond function.
      Click image for larger version

Name:	Picture1.png
Views:	1
Size:	44.9 KB
ID:	1782949



      I hope this clarifies. Many thanks.
      Zoé

      Comment


      • #4
        Please see FAQ Advice #12 for an explanation of why screenshots are not as helpful as you might think. The preferred way to share data examples, as explained in that FAQ, is by using the dataex command.


        Originally posted by Ourega-Zoe Ejebu View Post
        • The q_alt variable shows the chosen(0/1) Job choice (in duplicate format as the dataset has been expanded)
        Here is the main problem:[LIST][*]The q_dup variable should show the alternative Job choice; i.e. when Job B is chosen in row 1 (q_alt), q_dup should show Job A in row 2[*]Similarly, when 'My previous choice' is chosen in row 3 (q_alt), in row 4 'My current job' should be displayed.

        Your -cond()- function in #1 is not exhaustive. In addition, when sorting on the variable choiceset, it is not clear that chosen=0 will be always ordered second. Generally, it's not a good idea to specify a condition that depends on the sort order of observations. I may not yet have fully understood the task, but my guess is that you want to do the following in the final merged dataset:

        Code:
        bysort id choiceset (chosen): replace q_dup=cond(q_alt[_n+1]==1, 2, 4) if inlist(q_alt[_n+1], 1, 3)
        gsort id choiceset -chosen
        assuming

        the variables take the following numerical values:
        • Job A=1
        • Job B=2
        • Previous Choice=3
        • Current Job=4
        and the chosen job is either Job B or Previous Choice. If the chosen job is neither of these, you need to clarify the replacement rule. See https://journals.sagepub.com/doi/pdf...867X0500500310 for a tutorial on the -cond()- function.
        Last edited by Andrew Musau; 06 Nov 2025, 08:45.

        Comment


        • #5
          Thanks Andrew.

          I was able to find a solution after all, using the following syntax:

          by id choiceset: replace q_dup=cond(q_alt==1, 2, 1) if _n==2 & q_dup!=3 & q_dup!=4 & q_dup!=.
          by id choiceset: replace q_dup=cond(q_alt==3, 4, 3) if _n==2 & q_dup!=1 & q_dup!=2 & q_dup!=.

          Many thanks.
          Zoé

          Comment

          Working...
          X