S.Sakha
S.Sakha

Reputation: 3

Generate variables by many different groups

I have a dataset with:

  1. A unique person_id.
  2. Different subjects that the person took in the past (humanities, IT, business etc.).
  3. The Degree of each subject.

This looks as follows:

person_id humanities business IT   Degree
1           0       1         0     BSc
1           0       0         1     MSc
2           1       0         0     PhD
2           0       1         0     MSc
2           0       0         1     BSc
3           0       0         1     BSc

I would like to transform this dataset so that I have variables consisting of each possible combination of degree and subject for each person_id.

The idea is that when I collapse it later by person_id, I will have one value for each person (namely 0 or 1). I have twelve different subjects and four main degrees.

person_id humanities business IT   Degree  BSc_humanities  MSc_Hum 
1           0          1       0    BSc              0         0
1           0          0       1    MSc              0         0
2           1          0       0    PhD              0         1
2           1          0       0    MSc              0         1
2           0          0       1    BSc              0         1
3           0          0       1    BSc              0         0

What would be the best possible way to achieve this?

Upvotes: 0

Views: 62

Answers (1)

user8682794
user8682794

Reputation:

You could use fillin:

clear

input person_id humanities business IT  str3 Degree
1           0       1         0     BSc
1           0       0         1     MSc
2           1       0         0     PhD
2           0       1         0     MSc
2           0       0         1     BSc
3           0       0         1     BSc
end

fillin person_id humanities business Degree

list person_id humanities business Degree

     +-----------------------------------------+
     | person~d   humani~s   business   Degree |
     |-----------------------------------------|
  1. |        1          0          0      BSc |
  2. |        1          0          0      MSc |
  3. |        1          0          0      PhD |
  4. |        1          0          1      BSc |
  5. |        1          0          1      MSc |
     |-----------------------------------------|
  6. |        1          0          1      PhD |
  7. |        1          1          0      BSc |
  8. |        1          1          0      MSc |
  9. |        1          1          0      PhD |
 10. |        1          1          1      BSc |
     |-----------------------------------------|
 11. |        1          1          1      MSc |
 12. |        1          1          1      PhD |
 13. |        2          0          0      BSc |
 14. |        2          0          0      MSc |
 15. |        2          0          0      PhD |
     |-----------------------------------------|
 16. |        2          0          1      BSc |
 17. |        2          0          1      MSc |
 18. |        2          0          1      PhD |
 19. |        2          1          0      BSc |
 20. |        2          1          0      MSc |
     |-----------------------------------------|
 21. |        2          1          0      PhD |
 22. |        2          1          1      BSc |
 23. |        2          1          1      MSc |
 24. |        2          1          1      PhD |
 25. |        3          0          0      BSc |
     |-----------------------------------------|
 26. |        3          0          0      MSc |
 27. |        3          0          0      PhD |
 28. |        3          0          1      BSc |
 29. |        3          0          1      MSc |
 30. |        3          0          1      PhD |
     |-----------------------------------------|
 31. |        3          1          0      BSc |
 32. |        3          1          0      MSc |
 33. |        3          1          0      PhD |
 34. |        3          1          1      BSc |
 35. |        3          1          1      MSc |
     |-----------------------------------------|
 36. |        3          1          1      PhD |
     +-----------------------------------------+

Upvotes: 1

Related Questions