Announcement

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

  • reshape the dataset

    Hello

    I have the dataset as below. It is just one long row variable. I want to reshape using reshape long bV, i(group) j(year).
    bV_1998_g1..... 1998 is year and is until 2010 and g1-g17 is a group. But there is no year and group variable so,this is not set as a time series variable.
    How can I reshape this? Thank you!
    Attached Files

  • #2
    Code:
    gen long obs_no = _n
    reshape long bv_, i(obs_no) j(_j) string
    split _j, parse("_"), gen(jj) destring
    rename jj1 year
    rename jj2 group
    drop obs_no _j
    This code is untested because data examples from screenshots are not importable to Stata for testing.

    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Hello,

      Thank you. I managed to do reshape. However, the purpose of reshaping was to plot them, but when I try the codes as below.
      graph twoway///
      line bV_ year if group_== 1 || ///
      line bV_ year if group_== 2 || ///
      line bV_ year if group_== 3 ||

      it does show error 'graph specification required'. I want to have every group trends in one graph. Could you help me? Thank you!!

      (I don't know how to put codes in the grey box so here it goes.)
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float bV_ int year long group_
        .0010730845 1998 10
        .0015603466 1998 11
      .000031539985 1998 15
        .0007524692 1998  5
          .00240441 1998  4
          .00821252 1998  1
         .006289936 1998 16
        .0003136269 1998 13
         .007320995 1998  .
        .0013868528 1998 17
       .00052119535 1998 12
        .0008915251 1998  7
         .007172044 1998  3
         .004973455 1998  6
        .0003294309 1998  2
        .0001930298 1998 14
        .0009088331 1999  5
          .00476384 1999  .
        .0011934263 1999  4
        .0026034166 1999 16
        .0004747722 1999 10
      2.5342015e-06 1999 17
         .008141396 1999  3
          .00485471 1999  1
       .00009086983 1999  8
       .00007111559 1999 14
       .00009829163 1999 12
        .0005964168 1999 13
         .003943128 1999  6
        .0006232896 1999 11
        .0006363565 1999 15
        .0005844737 1999  7
        .0013964748 1999  2
          .00564572 2000  6
        .0003117845 2000 15
       .00003497129 2000 12
        .0001104193 2000  7
        .0023345484 2000 16
        .0012149155 2000 11
        .0004415413 2000 10
        .0001464953 2000 17
       .00012560179 2000  2
        .0000505212 2000  5
         .006153188 2000  .
         .006263608 2000  1
       .00011160676 2000 13
        .0008536011 2000 14
         .004320632 2000  3
         .003605635 2000  4
         .005396792 2001  .
        .0005419467 2001 15
       .00005195029 2001  7
         .002818998 2001  3
      1.5277893e-08 2001  8
         .005396807 2001  1
         .002309198 2001 16
         .004253245 2001  6
         .003263295 2001  4
        .0017497336 2001 14
        .0001477856 2001 11
        9.11285e-06 2001 13
        .0003495851 2001  2
       .00051729055 2001 10
       .00005348838 2001 12
      2.3245234e-06 2001 17
        .0003293121 2001  5
         .003175748 2002 16
         .007377964 2002  4
         .006458694 2002  .
        .0000457519 2002 10
       .00003686076 2002 13
        .0019012033 2002  3
        .0015811046 2002 11
         .002622449 2002 15
        .0008973099 2002  5
       .00058119756 2002 14
         .006518673 2002  1
       .00005997856 2002  8
         .007025526 2002  6
         .002338664 2002 17
       .00003561041 2002  7
        .0003082562 2002 12
        .0003828568 2002  2
         .012010995 2003  6
      .000027860526 2003  8
      .000014267325 2003 13
        .0001282375 2003  2
        .0003343583 2003  5
        .0002702422 2003 17
        .0031425345 2003  3
       .00023634816 2003 12
         .002435399 2003  4
         .003823646 2003 16
        .0042339335 2003  .
        .0007625222 2003 10
        .0002698126 2003  7
         .004261794 2003  1
        .0018264236 2003 11
        .0021286313 2003 15
        .0006946605 2003 14
      end
      label values group_ group_
      label def group_ 1 "g1", modify
      label def group_ 2 "g10", modify
      label def group_ 3 "g11", modify
      label def group_ 4 "g12", modify
      label def group_ 5 "g13", modify
      label def group_ 6 "g14", modify
      label def group_ 7 "g15", modify
      label def group_ 8 "g16", modify
      label def group_ 10 "g2", modify
      label def group_ 11 "g3", modify
      label def group_ 12 "g4", modify
      label def group_ 13 "g5", modify
      label def group_ 14 "g6", modify
      label def group_ 15 "g7", modify
      label def group_ 16 "g8", modify
      label def group_ 17 "g9", modify

      Comment


      • #4
        You have at least 16 groups there. Judging by the value labels they aren't in correct order.

        Superimposed -- graphs on top of each other -- you will have a mess, and the legend will grab much of the space too. Juxtaposed -- graphs side by side -- it's hard work to compare.

        One way forward is to try a fabplot (front-and-back plot). Code from SSC. Story at https://www.statalist.org/forums/for...e-on-ssc/page2

        Code:
        gen group = cond(group_ == 1, 1, cond(inrange(group_, 10, 17), group - 8, group + 8))
        
        fabplot line bV_ year, by(group, compact note("")) frontopts(lw(thick)) xla(1998/2003, format(%tyY)) name(G1, replace) 
        
        fabplot line bV_ year, by(group, compact note("")) frontopts(lw(thick)) xla(1998/2003, format(%tyY)) ///
        ysc(log) yla(1e-7 1e-6 1e-5 1e-4 1e-3 1e-2, ang(h))  name(G2, replace)
        Click image for larger version

Name:	young_1.png
Views:	1
Size:	79.1 KB
ID:	1491128


        Click image for larger version

Name:	young_2.png
Views:	1
Size:	146.1 KB
ID:	1491129

        Comment


        • #5
          If in post #3 if you had copied the command and its output from your Results window and pasted it into your post using CODE delimiters, we would have seen
          Code:
          . graph twoway///
          graph specification required
          r(198);
          and would have realized that you neglected to include a space between twoway and the continuation mark in the first line of your command. Your command should read
          Code:
          graph twoway ///
          line bV_ year if group_== 1 || ///
          line bV_ year if group_== 2 || ///
          line bV_ year if group_== 3
          If you don't know how to post code in this fashion, you should take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

          Section 12.1 is particularly pertinent

          12.1 What to say about your commands and your problem

          Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
          To assure maximum readability of results that you post, please copy them from the Results window or your log file into a code block in the Forum editor using code delimiters [CODE] and [/CODE], as explained in section 12 of the Statalist FAQ linked to at the top of the page. For example, the following:

          [CODE]
          . sysuse auto, clear
          (1978 Automobile Data)

          . describe make price

          storage display value
          variable name type format label variable label
          -----------------------------------------------------------------
          make str18 %-18s Make and Model
          price int %8.0gc Price
          [/CODE]

          will be presented in the post as the following:
          Code:
          . sysuse auto, clear
          (1978 Automobile Data)
          
          . describe make price
          
                        storage   display    value
          variable name   type    format     label      variable label
          -----------------------------------------------------------------
          make            str18   %-18s                 Make and Model
          price           int     %8.0gc                Price

          Comment

          Working...
          X