Search code examples
stata

Split IDs in categorical variables


I have a variable with IDs:

clear

input ID
1
.
2
1
.
3
4
5
4
4
6
end

How can I create separate categorical variables with ID as a name and values of 1 and 2 (the latter if the generated variable matches the ID)?

For example, variable _ID_1 should look as follows:

2
.
1
2
.
1
1
1
1
1
1

Any ideas?


Solution

  • Another way to do it:

    clear
    
    input ID
    1
    .
    2
    1
    .
    3
    4
    5
    4
    4
    6
    end
    
    forvalues j = 1/6 { 
        generate ID_`j' = 1 + (ID == `j') if ID != . 
    } 
    
    list