Home > Mobile >  Reshape Data from Long to Wide without identifier in Stata
Reshape Data from Long to Wide without identifier in Stata

Time:09-27

How can I reshape the dataset below from long to wide in Stata?

a1      a2
NAME    Jane
SEX     female
PHONE   234
SCORE   9
NAME    John
SEX     male
PHONE   444
SCORE   10
NAME    Baba
SEX     male
PHONE   777
SCORE   5

I've tried using gen i = tag(a1) to generate an id. However, this does not uniquely identify each set of repeating data.

CodePudding user response:

You're correct that you need an identifier, indeed two identifiers. But gen i = tag(a1) is illegal -- presumably you mean egen -- and more to the point is not going to help. egen, tag() depends on identifiers existing already and serves only to create a (0, 1) variable, not what you need here.

This works for me. Please note the use of Stata code to create a data example: there is much more on this at the Stata tag wiki.

clear 
input str6 (a1      a2) 
NAME    Jane
SEX     female
PHONE   234
SCORE   9
NAME    John
SEX     male
PHONE   444
SCORE   10
NAME    Baba
SEX     male
PHONE   777
SCORE   5
end 

drop a1
egen id = seq(), block(4)
egen j = seq(), to(4)
reshape wide a2, i(id) j(j)
rename (a2*) (name sex phone score)
destring, replace 

list 

      ------------------------------------ 
     | id   name      sex   phone   score |
     |------------------------------------|
  1. |  1   Jane   female     234       9 |
  2. |  2   John     male     444      10 |
  3. |  3   Baba     male     777       5 |
      ------------------------------------ 
  • Related