Announcement

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

  • How to reshape from long to wide in stata

    Hi Statalist,

    This is my first post.

    I have a single variable called random_variable containing random numbers, say, from 1 to 100 but may not be sequentially ( maybe 1, 7, 2). I want to create 15 new variables in the data set each one containing the first 7 (7 may be arbitrary, example is for weekdays) entries from random_variable except the 15th one which will contain only 2 entries. (14*7=98 + 2 in last column).

    example
    random_variable
    1
    7
    2
    .
    .
    .
    up to 100 observations.
    .


    Anticipating your help

    Ashish

  • #2
    Code:
    forvalues i = 1/15 {
        gene new_`i'  = cond(_n<=7,random_variable,.)
        }
        replace new_15 = . if _n>2
    Hope this helps!

    Tiago

    Comment


    • #3
      Tiago Pereira, Thanks for reply . The code you shared is not working. For Simplicity I have kept 0nly 18 observation in random_variable and tried for 3 new variables having 7 observations each (7*2=14+4 in last variable)

      forvalues i = 1/3 {
      gene new_`i' = cond(_n<=7,random_variable,.)
      }
      replace new_3 = . if _n>4



      Please have a look

      clear
      input double random_variable
      1
      2
      5
      7
      10
      11
      34
      56
      23
      31
      89
      96
      56
      45
      38
      90
      13
      21
      end

      Actual Outcome

      random_variable new_1 new_2 new_3
      1 1 1 1
      2 2 2 2
      5 5 5 5
      7 7 7 7
      10 10 10 10
      11 11 11 11
      34 34 34 34
      56
      23
      31
      89
      96
      56
      45
      38
      90
      13
      21


      Expected Outcome


      random_variable new_1 new_2 new_3
      1 1 56 38
      2 2 23 90
      5 5 31 13
      7 7 89 21
      10 10 96
      11 11 56
      34 34 45
      56
      23
      31
      89
      96
      56
      45
      38
      90
      13
      21


      Ashish

      Comment


      • #4
        You were not clear at the beginning.
        "containing the first 7 (7 may be arbitrary, example is for weekdays)"

        So, containing 7 numbers chosen at random.



        Code:
        clear
        input double random_variable
        1
        2
        5
        7
        10
        11
        34
        56
        23
        31
        89
        96
        56
        45
        38
        90
        13
        21
        end
        
        
            forvalues i = 1/15 {
                preserve
                sample 7, count
                tempfile new_`i'
                rename random_variable new_`i'
                save `new_`i'', replace
                restore
            }
        
            forvalues i = 1/15 {
            merge 1:1 _n  using `new_`i''
            cap drop _merge
            }

        Comment


        • #5
          random.docxSorry Tiago Pereira for not clarity. In my question. In your reply #4, You have mentioned the command sample that I don't want. I want to generate 15 new variables each containing 7 observations from the random_variable that contains 100 random observations. So the first new variable should contain the first 7 observations from the random variable, the second new variable should contain the next 7 observations from the random variable, and so on. I have attached the image file for simplicity .

          Comment


          • #6
            Code:
            clear
            set obs 105
            gene random_variable = _n
            gene x = runiform()
            sort x
            cap drop x
            seq group, f(1) t(15) b(7)
            sort group
            
                forvalues i = 1/15 {
                preserve
                keep if group == `i'
                cap drop group
                tempfile new_`i'
                rename random_variable new_`i'
                save `new_`i'', replace
                restore
                }
                
                
                
                forvalues i = 1/15 {
                merge 1:1 _n  using `new_`i''
                cap drop _merge
                }
                
                cap drop group

            Comment


            • #7
              Tiago Pereira , Thank you for continuous support, I tried but found an error in the code "seq group, f(1) t(15) b(7)". Stata does not recognize this seq command. is it seq () ?

              Comment


              • #8
                seq is a community-contributed command from 1997 and was in essence superseded by the seq() function of egen.

                Code:
                .     search dm44, entry historical
                
                Search of official help files, FAQs, Examples, and Stata Journals
                
                STB-37  dm44  . . . . . . . . . . . . . . . . . . . . .  Sequences of integers
                        (help seq if installed) . . . . . . . . . . . . . . . . . .  N. J. Cox
                        5/97    pp.2--4; STB Reprints Vol 7, pp.32--33
                        egen now has seq() function

                Comment


                • #9
                  You can adapt the code following Nick's wise advise. Alternatively, you can type:

                  Code:
                   ssc install seq

                  Comment


                  • #10
                    Thank you both for helping me out.

                    Comment

                    Working...
                    X