Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Transpose Data from Wide to Long

    Hello, I am interested in making a basic transposition of my data so that the columns becomes the rows, while at the same time maintaining the value labels for the variables.

    Below is a copy the original data on the left side of the image, and my desired shape on the right.

    Click image for larger version

Name:	WideLong.jpg
Views:	1
Size:	55.4 KB
ID:	1531701


    Here is an extract of the data in STATA format:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double(id sex marital_Status sex_orient religion)
     1 1 1 3 3
     2 2 3 1 2
     3 2 1 1 4
     4 2 4 1 4
     6 2 1 1 4
     7 2 1 1 3
     8 2 1 1 4
     9 2 1 1 3
    10 2 3 1 3
    end
    label values sex sex
    label def sex 1 "Male", modify
    label def sex 2 "Female", modify
    label values marital_Status marital_Status
    label def marital_Status 1 "Married / Domestic Partnership", modify
    label def marital_Status 3 "Divorced", modify
    label def marital_Status 4 "Separated", modify
    label values sex_orient sex_orient
    label def sex_orient 1 "Heterosexual (straight)", modify
    label def sex_orient 3 "Bisexual", modify
    label values religion religion
    label def religion 2 "Catholic", modify
    label def religion 3 "Protestestant", modify
    label def religion 4 "Muslim", modify
    I tried to use the reshape command, but it seems as if it is meant for repeated events. since this is not repeated data, I am at a loss as to how to handle it.

    Thanks again, cY

  • #2
    -help xpose-

    Comment


    • #3
      Clyde. thanks. i tried the -xpose- command, biut it doesn't transfer the value labels. is there any other way this could be accomplished?

      with appreciation, cY

      Comment


      • #4
        I don't think there is any way you can carry across the value labels in a Stata data set. You could, of course, -decode- the original variables and then you could transpose that by double-reshaping the data:

        Code:
        ds, has(vallabel ?*)
        foreach v of varlist `r(varlist)' {
            decode `v', gen(_`v')
            order _`v', before(`v')
            drop `v'
        }
        rename _* *
        list
        
        ds id, not
        rename (`r(varlist)') _=
        reshape long _, i(id) j(varname) string
        reshape wide _, i(varname) j(id)
        But you do not have labeled integer variables in the resulting dataset: it is a data set consisting entirely of strings.

        The reason you can't do what you want is that there is an asymmetry between observations and variables in Stata. variables can have value labels, but observations cannot. This is different from a spreadsheet where there is no inherent asymmetry between rows and columns because each cell can be idiosyncratically typed without regard to what else appears in the same row or column.

        Comment


        • #5
          Yawo Kokuvi -

          Perhaps you should take a step back and explain what it is you hope to accomplish by turning your variables into observations and your observations into variables. It's generally not a useful format for Stata analysis, as Clyde implies. I have the feeling your actual goal - the reason you think you need to transpose your Stata dataset - can be more easily accomplished by taking a different path.

          Comment

          Working...
          X