Announcement

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

  • Move variable names to 1 row observations

    I would like to move variable names to the 1st row observations (as string)

    In the following example, I would like the variable dates to become the first row of observations (the new names of the columns do not matter)
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 DATATYPE double(__7_8_2020 __7_1_2020 _6_24_2020 _6_17_2020 _6_10_2020 __6_3_2020 _5_27_2020 _5_20_2020 _5_13_2020)
    "SP500_w" 6496.14 6383.76 6247.66 6375.54 6529.42 6389.67 6209.38 6075.76 5761.7
    "VIX_w"     28.08   28.62   33.84   33.47   27.57   25.66   27.62   27.99  35.28
    end
    In other words, I'm trying the opposite of what "nrow" does. Please help.

  • #2
    May I ask why you need variable names in the first row? Doing that will make your dataset pretty much useless for Stata, since all your variables will converted to string variables so you cannot use these anymore for calculations.

    If you want to export to Excel and have the variable names in the first row you can use the firstrow option of export excel.

    Comment


    • #3
      I want it this way because I then want to use the dates to transpose the dataset (using 2x reshape).

      Any idea? I am not seeking to use another track to solve the problem. Thanks.

      Comment


      • #4
        I don't see the need to have the variable names in the first observation then. It looks like Nick Cox already posted a solution here.

        Comment


        • #5
          I am with Wouter Wakker on this. The only Stata problem helped by doing this is export elsewhere, which as said is better done otherwise.

          Comment


          • #6
            Wouter Wakker please do not respond to my posts if not to offer a concrete solution, it prevent other people that may have a solution to look at the problem. But I do appreciate your help if you can offer a solution.

            Anyone knows the answer? Thanks a lot in advance.

            Comment


            • #7
              It often happens that people ask for help with A to get to B, but A turns out not to be a good way to get to B, and the solution to get to B is to do something different than A. My intention in #2 and #4 was to get you a solution to B, which seemed to have been offered in the post linked to in #4 (although you have ignored this comment so I can't be sure).

              However, you still insist on doing A, so here you go:
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input str8 DATATYPE double(__7_8_2020 __7_1_2020 _6_24_2020 _6_17_2020 _6_10_2020 __6_3_2020 _5_27_2020 _5_20_2020 _5_13_2020)
              "SP500_w" 6496.14 6383.76 6247.66 6375.54 6529.42 6389.67 6209.38 6075.76 5761.7
              "VIX_w"     28.08   28.62   33.84   33.47   27.57   25.66   27.62   27.99  35.28
              end
              
              insobs 1, before(1)
              foreach var of varlist _* {
                  tostring `var', replace
                  replace `var' = "`var'" in 1
              }
              
              list, noobs
              
                +-------------------------------------------------------------------------------------------------------------------------------+
                | DATATYPE   __7_8_2020   __7_1_2020   _6_24_2020   _6_17_2020   _6_10_2020   __6_3_2020   _5_27_2020   _5_20_2020   _5_13_2020 |
                |-------------------------------------------------------------------------------------------------------------------------------|
                |            __7_8_2020   __7_1_2020   _6_24_2020   _6_17_2020   _6_10_2020   __6_3_2020   _5_27_2020   _5_20_2020   _5_13_2020 |
                |  SP500_w      6496.14      6383.76      6247.66      6375.54      6529.42      6389.67      6209.38      6075.76       5761.7 |
                |    VIX_w        28.08        28.62        33.84        33.47        27.57        25.66        27.62        27.99        35.28 |
                +-------------------------------------------------------------------------------------------------------------------------------+

              Comment


              • #8
                Thank you so much Wouter, this is exactly what I needed!

                Comment

                Working...
                X