## How to generate dummy variables based on multiple if criteria

on 11 May 2019

on 13 May 2019

### Raj (view profile)

I need to generate a few dummy variables and would like your input on this.
In the dataset, there are 10 observations per participant and each participant is allocated to one of the four treatments (1,2,3,4). The choice is to select either "1" or "2" in 10 tasks (taskno). Below are the first few observations.
Now, I would like to generate a dummy variable, let's call it dummy_1, such that once the participant selects the choice 2 and the treatno is 1, then the dummy_1 should be equal to 1 for all the remaining observations (taskno) for the same participant.
For instance, in the above example, participant 1 selected choice 2 in the second task. Now for rest of the observations (taskno: 3 to 10) for participant 1, the dummy_1 should be equal to 1 (irrespective of participant 1 choices in taskno 3 to 10). The same would apply to participant 2 and so on.
The output should be:
Any help in this regard would be appreciated. Thanks.

### Raj (view profile)

on 13 May 2019

Lets say you have 10 participants, so a total of 100 dataset. I'll assume you already have your id, taskno, treatno and choice as column vectors. Now you have to create another vector dummy_1 (all zeros initially) which will be populated based on the condition fulfilment as given in your problem statement. Then a conditional loop something like this should work:
n=1;
for i=10:10:100
for j=n:i
if treatno(j,1)==1 && choice(j,1)==2
for m=j+1:i
dummy_1(m,1)=1;
end
n=n+10;
break
end
end
end
Now since I dont have the data, I have not tested it. I just typed it based on context. Hope this works! Let me know if you get stuck or the code throws any error.