Hello,
I hope this message finds you well.
I received a dataset with several multiple choice questions for which the selected answers by respondents are written into the same cell and separated by a comma (sometimes a comma is used in between parentheses within an answer choice to show examples). I am trying to separate the multiple answers into variables. Variable1 should show me all the respondents that selected answer choice1, variable2 should show me all the respondents that selected answer choice2...
All variables are string so I tried splitting using parse(,). Which works to split but it takes the first answer from each respondent and puts it in the first variable, then the 2nd answer in the 2nd variable. Hence, not exactly what I am looking for. In my data example below I show the results of using split and parse in am1 am2 am3 am4 am5 am6, and in lundi_am and mardi_am I show what I would like to be able to get with some code (I wrote it by hand for this example). In lundi_am, all the respondents that selected lundi in jour_am received a numeric value of 1, they received a 0 if they selected a different answer from lundi, and a missing value if they selected nothing. In mardi_am, all the respondents that selected mardi in jour_am received a numeric value of 1, they received a 0 if they selected a different answer from mardi, and a missing value if they selected nothing.
Below is an example of data.
Please note that the variable jour_am is not my most complex variable in terms of multiple choice selections, I have three other variables that exceed the number of characters allowed in dataex for a line but in one variable. I am listing one of them below.
I will adapt the code to these other variables as I receive the below error message from dataex with one variable, but in your suggestion for a solution is it possible to differentiate between a comma to separate answers or a comma within an answer to list examples?
Best wishes and thank you for your thoughts on this matter,
Patrick
I hope this message finds you well.
I received a dataset with several multiple choice questions for which the selected answers by respondents are written into the same cell and separated by a comma (sometimes a comma is used in between parentheses within an answer choice to show examples). I am trying to separate the multiple answers into variables. Variable1 should show me all the respondents that selected answer choice1, variable2 should show me all the respondents that selected answer choice2...
All variables are string so I tried splitting using parse(,). Which works to split but it takes the first answer from each respondent and puts it in the first variable, then the 2nd answer in the 2nd variable. Hence, not exactly what I am looking for. In my data example below I show the results of using split and parse in am1 am2 am3 am4 am5 am6, and in lundi_am and mardi_am I show what I would like to be able to get with some code (I wrote it by hand for this example). In lundi_am, all the respondents that selected lundi in jour_am received a numeric value of 1, they received a 0 if they selected a different answer from lundi, and a missing value if they selected nothing. In mardi_am, all the respondents that selected mardi in jour_am received a numeric value of 1, they received a 0 if they selected a different answer from mardi, and a missing value if they selected nothing.
Code:
split jour_am, parse(,) generate(am)
Code:
* Example generated by -dataex-. For more info, type help dataex clear input float id str8 am1 str9(am2 am3 am4 am5) str7 am6 byte(lundi_am mardi_am) 1 "" "" "" "" "" "" . . 2 "Lundi" " Mardi" " Mercredi" " Vendredi" "" "" 1 1 3 "Mardi" "" "" "" "" "" 0 1 4 "" "" "" "" "" "" . . 5 "Mercredi" "" "" "" "" "" 0 0 6 "Lundi" " Mardi" " Mercredi" " Jeudi" " Vendredi" "" 1 1 7 "" "" "" "" "" "" . . 8 "" "" "" "" "" "" . . 9 "Mercredi" "" "" "" "" "" 0 0 10 "" "" "" "" "" "" . . 11 "Mardi" " Jeudi" "" "" "" "" 0 1 12 "Lundi" " Mardi" " Mercredi" " Jeudi" "" "" 1 1 13 "" "" "" "" "" "" . . 14 "Mardi" " Jeudi" " Vendredi" "" "" "" 0 1 15 "" "" "" "" "" "" . . 16 "" "" "" "" "" "" . . 17 "" "" "" "" "" "" . . 18 "Lundi" "" "" "" "" "" 1 0 19 "" "" "" "" "" "" . . 20 "" "" "" "" "" "" . . 21 "Lundi" " Mardi" " Mercredi" " Jeudi" "" "" 1 1 22 "" "" "" "" "" "" . . 23 "Mardi" "" "" "" "" "" 0 1 24 "Jeudi" "" "" "" "" "" 0 0 end
list act
+--------------------------------------------------------------------------------------------------------------------------------+
| act |
|--------------------------------------------------------------------------------------------------------------------------------|
1. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Conférences ou.. |
2. | Atelier de la mémoire et boxe |
3. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.) |
4. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.), Conférences ou re.. |
5. | Conférences ou rencontres d'information |
|--------------------------------------------------------------------------------------------------------------------------------|
6. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
7. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.), Conférences ou re.. |
8. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.), Conférences ou re.. |
9. | |
10. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Déjeuners/Acti.. |
|--------------------------------------------------------------------------------------------------------------------------------|
11. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
12. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
13. | Cafés rencontres avec thème, Déjeuners/Activités sociales (activités de Noël, pique-nique fin d'année etc.) |
14. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
15. | Conférences ou rencontres d'information, Groupes de soutien pour personnes atteintes
+--------------------------------------------------------------------------------------------------------------------------------+
| act |
|--------------------------------------------------------------------------------------------------------------------------------|
1. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Conférences ou.. |
2. | Atelier de la mémoire et boxe |
3. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.) |
4. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.), Conférences ou re.. |
5. | Conférences ou rencontres d'information |
|--------------------------------------------------------------------------------------------------------------------------------|
6. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
7. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.), Conférences ou re.. |
8. | Activités physiques vigoureuses (ex. : boxe, cardio musculation, cardio plein air, marche nordique, etc.), Conférences ou re.. |
9. | |
10. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Déjeuners/Acti.. |
|--------------------------------------------------------------------------------------------------------------------------------|
11. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
12. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
13. | Cafés rencontres avec thème, Déjeuners/Activités sociales (activités de Noël, pique-nique fin d'année etc.) |
14. | Activités physiques légères à modérées (ex. : Tai Chi, danse adaptée, flexibilité/stretching postural, etc.), Activités phys.. |
15. | Conférences ou rencontres d'information, Groupes de soutien pour personnes atteintes
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str506 act data width (508 chars) exceeds max linesize. Try specifying fewer variables r(1000);
Patrick
Comment