Announcement

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

  • changing rows into 2 columns

    Hi guys,

    I am new to stata and currently have a dataset with 50 columns but only one observation in each column. I would like to change data into two columns with the first 25 columns being merged into one column and the next 25 into the other. I have used the xpose function but it gives me the observations all in one column.

    Is there another function I can use ?

  • #2
    So, using Stata terminology, you have a single observation with 50 variables and you want to transform this into a data set with 25 observations and 2 variables. You don't tell us what the 50 variables are called. If they are something like var1 through var50 it's easier, so I'm going to assume it's that. If it isn't, post back with more information. (Specifically, the output from -des- would be helpful. To be sure what you post is complete and accurate in detail, copy the output directly from Stata's Results window or log file to the clipboard and then paste into a code block here. If you don't know how to set up a code block, see FAQ #12 7th paragraph.)

    Code:
    set obs 25
    gen v1 = .
    gen v2 = .
    forvalues i = 1/25 {
        replace v1 = var`i'[1] in `i'
        replace v2 = var`=`i'+25'[1] in `i'
    }
    
    drop var*
    Note: This assumes that all 50 of your variables are numeric. If they are all strings, replace -gen v1 = .- by -gen v1 = ""-, likewise for v2. If the first 25 are strings and the next 25 are numeric, then do that for v1 only; vice versa leads to vice versa. If you have a mixture of strings and numeric variables other than one of these situations, then what you ask for cannot be done because in Stata, unlike a spreadsheet, you cannot have both string and numeric in the same variable (column).

    Comment


    • #3
      Every program has its jargon to learn.

      In Stata, columns are called variables and rows are called observations. xpose is a command, not a function,

      As you want your data to be mapped into two main variables, you need to apply the reshape command.

      Here's one way to do it. I first set up an example dataset with your problem. I can't use your variable names, because you don't tell us what they are, By all means tell us about your variable names and/or show the data to get more specific advice.

      Code:
      clear 
      set obs 1
      forval j = 1/50 {
      gen x`j' = 42
      }
      gen id = 1
      reshape long x, i(id)
      drop id 
      gen col = _j > 25
      replace _j = _j - 25 if _j > 25 
      reshape wide x, i(_j) j(col) 
      list

      Comment


      • #4
        Thank you very much for your responses I really appreciate the edification

        Comment


        • #5
          Hi guys,

          This is what it looks like:

          I have 10 variables here and only one observation in each. So I would like two variables- one containing the standard deviation (sd) and the other containing the returns.

          Thank you for your help.


          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float(sd_1 sd_2 sd_3 sd_4 sd_5 return_1 return_2 return_3 return_4 return_5)
          4.5705657 4.5493298 4.5289125 4.509326 4.490475 .01 .02 .03 .04 .05
          end

          Comment


          • #6
            Code:
            clear
            input float(sd_1 sd_2 sd_3 sd_4 sd_5 return_1 return_2 return_3 return_4 return_5)
            4.5705657 4.5493298 4.5289125 4.509326 4.490475 .01 .02 .03 .04 .05
            end
            gen id = 1 
            reshape long return_ sd_, i(id) 
            list

            Comment


            • #7
              Than you

              Comment

              Working...
              X