Elise
Elise

Reputation: 87

reshape wide to long in stata but new variable contains all missing values

I need to reshape a dataset, whose original form is as follows:

schid     m2s1q0_i     m2s1q0_ii    ...     m2s1q0_x
1            6            2                   3

data screenshot I want to reshape it into the long format, like this:

schid teacher_id
1        5
1        2
...      
1       3

I used this code:

reshape long m2s1q0_, i(schoolid) j(teacher_id)

However, the teacher_id variable is all missing. Where did it go wrong?

Upvotes: 0

Views: 8087

Answers (1)

Eric HB
Eric HB

Reputation: 887

If you use the option string teacher ids will be generated as string variables rather than missing. You can then use encode to create numeric values for the teacher_id variable

Here is an example:

clear
set obs 10
gen schid = _n
gen m_i = 1
gen m_ii = 2
gen m_iii = 3
reshape long m_, i(schid) j(teacher_id) string
encode teacher_id, gen(teacher_id2)

Upvotes: 3

Related Questions