Hello everyone,

I am looking for a way to create one categorical variable out of multiple dummy variables. More specifically, I have ten dummy variables indicating whether a person remembered a certain word named co07_1 co07_2 co07_3 co07_4 co07_5 co07_6 co07_7 co07_8 co07_9 co07_10. They are =1 if the respondent remembered the word and =3 otherwise.

I want to create a categorical variable that tells me how many words a person remembered ranging from 0 to 10.

If I were to do this by hand, this would require a lot of coding. Here an example of what this would look like for the values 9 and 10.

gen co07count = 10 if co07_1==1 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==1
replace co07count = 9 if co07_1==3 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==1 | co07_1==1 & co07_2==3 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==1 | co07_1==1 & co07_2==1 & co07_3==3 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==1 | co07_1==1 & co07_2==1 & co07_3==1 & co07_4==3 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==1 | co07_1==1 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==3 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==1 | co07_1==1 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==3 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==1 | co07_1==1 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==3 & co07_8==1 & co07_9==1 & co07_10==1 | co07_1==1 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==3 & co07_9==1 & co07_10==1 co07_1==1 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==3 & co07_10==1 | co07_1==1 & co07_2==1 & co07_3==1 & co07_4==1 & co07_5==1 & co07_6==1 & co07_7==1 & co07_8==1 & co07_9==1 & co07_10==3

Is there any way that one can use a loop for this that would (i) speed up the process and (ii) reduce the possibility that any errors occur?

Somebody's help with this would be very much appreciated!