Hello everyone,

I'm working on a dataset. As you can observe in the example below, I got two variables: ID and KEY. I need to change the data from the KEY variable into several new dichotomous variables. This has to be done by creating a new variable for each of the different values that KEY can take (about 30 values). In the end, I'd need to keep only one observation per ID, in order for me to merge the data with another dataset. Can you provide me with any suggestions? I was thinking of working with grups (using by) but I couldn't find out how. I know that my algorithm has to go though each of the observations in every ID group to find which KEY values are recorded and then replace the values in the corresponding new variables in all of the group, and finally collapse the dataset.

Thank you very much.

P.S.: This is my first post, but I have been reading this forum for a long time already, and have learned a lot from it. Thank you for that too!


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int(ID KEY)
 1   12
 1   14
 1   29
 1 9999
 2 9999
 3 9999
 4 9999
 5 9999
 6 9999
 7 9999
 8   14
 8   15
 9 9999
10 9999
11 9999
12 9999
13 9999
14 9999
15    6
15   11
15   12
15   13
15   14
15   18
15   21
15   26
15   29
15   34
16    6
16   12
16   14
16   15
16   17
16   26
16   29
16   34
16   35
17 9999
18 9999
19 9999
20 9999
21 9999
22   21
23 9999
24 9999
25 9999
26 9999
27 9999
28 9999
29    6
29   34
30    6
30   12
30   14
30   15
30   17
30   26
30   29
30   34
30   35
31 9999
32   14
32   34
33   21
34 9999
35    6
35   14
35   26
36   11
37 9999
38 9999
39 9999
40    6
40   12
40   14
40   15
40   17
40   26
40   29
40   34
40   35
41    6
41   11
41   12
41   13
41   14
41   18
41   21
41   26
41   29
41   34
42    6
42   12
42   14
42   15
42   17
42   26
42   29
42   34
42   35
end