Announcement

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

  • Wide to long reshape, hundreds of stubs

    Dear Statalist community,

    I fear this might be a simple question but I am having trouble finding an effective way of transforming my dataset from wide form to long form.

    My variables come from a panel dataset where the prefix cw08a and cw09b signify the year each question was asked, the suffix, which range from 001 to 525 indicate the question asked to individuals. I have been trying to reshape the data using my i variable which define the individual (nomem_encr). I am pretty sure that the code should look something like this:

    Code:
    reshape long 001-525 , i(nomem_encr) j(year)
    However, I am don't know how exactly to code the stub 001-525 without doing in manually one at a time. I have included some data from dataex if this helps explain my query, including the variable cw08a_m and cwo9b_m provide the year and month each interview was held:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double(nomem_encr cw08a002 cw08a004 cw08a310 cw08a_m cw09b002 cw09b004 cw09b310 cw09b_m)
    800033 1991 8 . 200804 1991 8 . 200904
    800042 1975 5 . 200804 1975 6 . 200904
    end
    label values cw08a004 cw08a004
    label def cw08a004 5 "5", modify
    label def cw08a004 8 "8", modify
    label values cw08a310 cw08a310
    label values cw09b004 cw09b004
    label def cw09b004 6 "6", modify
    label def cw09b004 8 "8", modify
    label values cw09b310 cw09b310
    Thank you in advance,
    Hugo

  • #2
    If you use basic syntax the reshape long command expects stubs, which are always prefixes not suffixes. The hyphen syntax to indicate variable ranges cannot be used in this context.

    There is an advanced syntax which allows more complicated calls.

    If this were my problem I would know more about the dataset. But on this information I would rename first. It sounds as if variables

    Code:
    cw08a??? cw09b???
    should be renamed say

    Code:
    v???cw08a  v???cw09b
    and then it will be easier to collect prefixes using the method explained in https://www.stata.com/support/faqs/d...-with-reshape/ where,one question is

    is there any easy way to reshape the data to long without typing all the stub names?

    Comment


    • #3
      Hi Nick,

      I just wanted to write back to you and say thank you. I took your advice and simply recode the variables v???_2008 v???_2009 and was then able to do the command relatively straight forwardly with the following code:

      Code:
      unab stubs : *2008
      local stubs : subinstr local stubs "2008" "", all
      reshape long `stubs', i(nomem_encr) j(Year)
      For what it is worth to people, after the pointer from Nick I was able to find similar queries to mine which had been asked in the past (showcasing that I could have perhaps spent longer trying to find the answer myself), in case someone comes across this post in the future more information is available within this community in the future, these links all ask similar questions:
      https://www.statalist.org/forums/for...ing-at-the-end
      https://www.statalist.org/forums/for...many-variables
      https://www.statalist.org/forums/for...-of-many-years
      https://www.stata.com/statalist/arch.../msg01060.html

      Best,

      Hugo

      Comment


      • #4
        Hello, I want a panel dataset for which i want to reshape my data from wide to long. can someone kindly tell me where I am going wrong
        My variables are listed hortizontally like-

        net_income [FY 2005] net_income [FY 2006] ......net_income [FY 2020] depreciation [FY 2005] depeciation [FY2006].....depreciation [FY 2020]etc further more variables.


        In the rows, I have the name of the country, name of from and year.

        I get an error message -

        no xij variables found
        You typed something like reshape wide a b, i(i) j(j).
        reshape looked for existing variables named a# and b# but could not find any.

        Kindly guide me, what codes should I be writing to create such a panel?
        Thank you.



        Comment


        • #5
          #4 We need to see a data example and your reshape command to explain definitively what you did wrong. Please read and act on https://www.statalist.org/forums/help#stata

          Comment


          • #6
            I am using Stata 16.
            Sir, please find attached this is an example from the bigger dataset I am using.

            I am trying to obtain the result in this way--
            Click image for larger version

Name:	data result.jpg
Views:	2
Size:	60.0 KB
ID:	1639623

            Request you to kindly help me with what codes I should be running?
            Attached Files

            Comment


            • #7
              That does not match the request in #5. Please read the linked FAQ entry and give an example using dataex and copy your reshape command to this forum. The link explains why Excel spreadsheets are deprecated. Would you send a Stata .dta file to an Excel forum?

              Otherwise put, you are asking why your reshape command does not work but you haven't told us what it is. This is on a level with "My washing machine does not work. Do you know why?".

              Comment


              • #8
                I am using Stata 16.
                Sir, please find attached this is an example from the bigger dataset I am using.

                I am trying to obtain the result in this way--
                Click image for larger version

Name:	data result.jpg
Views:	2
Size:	60.0 KB
ID:	1639636

                Request you to kindly help me with what codes I should be running?
                Attached Files

                Comment


                • #9
                  I can't think of a different way to explain what I am asking for the third time.

                  Comment


                  • #10
                    .
                    Last edited by NIck Stavr; 06 Dec 2021, 15:37.

                    Comment


                    • #11
                      Apologies for the above. Please find the data as follows--

                      Code:
                      * Example generated by -dataex-. To install: ssc install dataex
                      clear
                      input byte f_id str46 firm str24 classification str2 employees int(netinc_2005 netinc_2006 netinc_2007 netinc_2008) byte(depreciation_2005 depreciation_2006 depreciation_2007 depreciation_2008)
                       1 "@Road Software India Pvt. Ltd."                 "Information Technology  " "5"   52   55   58   59  3  5  9 10
                       2 "0.5Bn FinHealth Pvt. Ltd."                      "Information Technology  " "10" 100  101  145  135 50 55 52 52
                       3 "100percent Nourishment Private Limited"         "Consumer Staples  "       "23"  65   65   63   80 12 13 12 12
                       4 "100x Entrepreneur"                              "Communication Services  " "65"  64   29   28   45 25 25 24 20
                       5 "101 Digital Services India Private Limited"     "Communication Services  " "96"  68   59   48   47 36 35 36 37
                       6 "10i Commerce Services Private Limited"          "Consumer Discretionary  " "20"  69   58   89   59 45 48 40 30
                       7 "10X Smart Technologies Private Limited"         "Consumer Discretionary  " "23"  63  144  135  149 50 52 58 89
                       8 "11th Hour Innovations Private Limited"          "Consumer Discretionary  " "30" 100  120   98  125 20 30 63 30
                       9 "123 Startup Ventures Pvt. Ltd."                 "Information Technology  " "10"  66   78   89   89 22 23 23 25
                      10 "1314 Film Producers Llp"                        "Communication Services  " "22"  65   66   68   68 33 35 36 36
                      11 "135 Tech Labs Pvt. Ltd."                        "Communication Services  " "."   42   44   43   41 11 10  9  8
                      12 "1589 Hotels Private Limited"                    "Consumer Discretionary  " "."   39   47   89   48 14 15 15 14
                      13 "160by2 Networks Private Limited"                "Communication Services  " "."   51   50   35   37 12 10 11 10
                      14 "1908 eventures pvt. Ltd."                       "Consumer Staples  "       "."   48   39   38   36  8  7  8  9
                      15 "19miles Automobiles Technologies Pvt Ltd"       "Consumer Discretionary  " "14"  29   32   36   45  6  6  9  6
                      16 "19th Mile Capability Solutions Private Limited" "Information Technology  " "23"  88   89   99   95 23 36 35 36
                      17 "1MG Technologies Private Limited"               "Consumer Discretionary  " "25"  78   79   78   81 24 25 25 23
                      18 "1mgAyush.com"                                   "Consumer Discretionary  " "14"  59   87   96   98 24 25 26 20
                      19 "1neoteric Technology Solutions Pvt. Ltd."       "Information Technology  " "10" 988 1000 1450 1600 55 57 85 89
                      20 "1st Consult tech pvt ltd"                       "Health Care  "            "8"   35   40   42   43 10 10  9  9
                      end
                      This is the output I get

                      Code:
                      Listed 20 out of 20 observations
                      
                      . reshape long netinc, depreciation, i(firm) j(year)
                      invalid 'i' 
                      r(198);
                      
                      .
                      I want to create a panel data set and reshape the above from wide to long (I have 10 more variables in the complete data.) For this, I understand I should first create a new variable for both, netincome and depreciation?
                      ---
                      I also generate a new variable in the panel for savings = net income-depreciation, for which--

                      Code:
                       egen savings_2005= netinc_2005- depreciation_2005
                      unknown egen function netinc_2005-()
                      r(133);
                      
                      .
                      Kindly tell me, what is wrong with the function I have used and how should I be using it.

                      Sorry for the trouble. Would be grateful for any help.

                      Comment


                      • #12
                        Code:
                        reshape long netinc_ depreciation_,  i(f_id) j(year)
                        works with your data example (thanks for that). The first bug was a comma that was wrong. The second bug would have been not specifying the underscores.

                        Otherwise you were using egen when you needed generate.

                        Comment


                        • #13
                          Thank you so much for your help, Professor. I tried it and it worked.
                          Sorry again for my ignorance earlier.

                          Comment


                          • #14
                            Professor once again, I tried the code this time with a larger dataset and missing data points and I get this output--
                            Code:
                             reshape long netinc_ depreciation_ revenue_ , i(f_id)  j(year)
                            (note: j = 2005 2006 2007 2008 
                            variable id does not uniquely identify the observations
                                Your data are currently wide.  You are performing a reshape long.  You specified i(f_id) and j(year).  In the
                                current wide form, variable f_id should uniquely identify the observations.  Remember this picture:
                                long                                wide
                                    +---------------+                   +------------------+
                                    | i   j   a   b |                   | i   a1 a2  b1 b2 |
                                    |---------------| <--- reshape ---> |------------------|
                                    | 1   1   1   2 |                   | 1   1   3   2  4 |
                                    | 1   2   3   4 |                   | 2   5   7   6  8 |
                                    | 2   1   5   6 |                   +------------------+
                                    | 2   2   7   8 |
                                    +---------------+
                                Type reshape error for a list of the problem observations.
                            r(9);
                            
                            .
                            Here is my dataex file

                            Code:
                            * Example generated by -dataex-. To install: ssc install dataex
                            clear
                            input int f_id str100 firm double(netinc_2005 netinc_2006 netinc_2007 netinc_2008 depreciation_2005 depreciation_2006 depreciation_2007 depreciation_2008 revenue_2005 revenue_2006 revenue_2007 revenue_2008)
                              1 "4Doctor"                                                               . . . . . . . . . . . .
                              2 "8wood"                                                                 . . . . . . . . . . . .
                              3 "99.co"                                                                 . . . . . . . . . . . .
                              4 "9Lives"                                                                . . . . . . . . . . . .
                              5 "A. Krisnawan & Rekan"                                                  . . . . . . . . . . . .
                              6 "A. Krisnawan Budipracoyo CPA"                                          . . . . . . . . . . . .
                              7 "Abattoir Surya Jaya PT"                                                . . . . . . . . . . . .
                              8 "Abubakar Usman & Rekan"                                                . . . . . . . . . . . .
                              9 "Abumas Group"                                                          . . . . . . . . . . . .
                             10 "Acep Kusmayadi  Ak BAP"                                                . . . . . . . . . . . .
                             11 "Achmad, Rasyid, Hisbullah & Jerry"                                     . . . . . . . . . . . .
                             12 "Adaro Finance B.V."                                                    . . . . . . . . . . . .
                             13 "Adi Jimmy Arthawan"                                                    . . . . . . . . . . . .
                             14 "Adidas Sourcing Limited"                                               . . . . . . . . . . . .
                             15 "ADM Cocoa, PT"                                                         . . . . . . . . . . . .
                             16 "Adr Group Of Companies"                                                . . . . . . . . . . . .
                             17 "Advance Career International"                                          . . . . . . . . . . . .
                             18 "AF Rachman & Soetjipto WS"                                             . . . . . . . . . . . .
                             19 "AFG Distribution Services Pte Ltd."                                    . . . . . . . . . . . .
                             20 "Agate Resources Indonesia PT"                                          . . . . . . . . . . . .
                             21 "Agate Studio"                                                          . . . . . . . . . . . .
                             22 "Agrindo Indonesia Jaya PT"                                             . . . . . . . . . . . .
                             23 "Agrindo Pratama PT"                                                    . . . . . . . . . . . .
                             24 "Agro Abadi, Pt"                                                        . . . . . . . . . . . .
                             25 "Air Energi Pacifica"                                                   . . . . . . . . . . . .
                             26 "Airmancur"                                                             . . . . . . . . . . . .
                             27 "AiSensum"                                                              . . . . . . . . . . . .
                             28 "Ajeindonesia, PT."                                                     . . . . . . . . . . . .
                             29 "Ajita"                                                                 . . . . . . . . . . . .
                             30 "Aker Solutions, Pt"                                                    . . . . . . . . . . . .
                             31 "Akhyadi  &  Chris"                                                     . . . . . . . . . . . .
                             32 "ALAMI SHARIA"                                                          . . . . . . . . . . . .
                             33 "Albert Silalahi & Rekan"                                               . . . . . . . . . . . .
                             34 "Alfa Group"                                                            . . . . . . . . . . . .
                             35 "Ali Budiardjo Nugroho Reksodiputro"                                    . . . . . . . . . . . .
                             36 "Alkemis Games Pte. Ltd."                                               . . . . . . . . . . . .
                             37 "Amachi Arifin Mardani & Muliadi"                                       . . . . . . . . . . . .
                             38 "Amcor Flexibles Indonesia, Pt."                                        . . . . . . . . . . . .
                             39 "Amerada Hess Indonesia Holdings Ltd."                                  . . . . . . . . . . . .
                             40 "Amir Abadi Jusuf & Aryanto"                                            . . . . . . . . . . . .
                             41 "Anadarko Ambalat Limited"                                              . . . . . . . . . . . .
                             42 "Anadarko Bukat Limited"                                                . . . . . . . . . . . .
                             43 "Anadarko Indonesia Company"                                            . . . . . . . . . . . .
                             44 "Anadarko Indonesia Nunukan Company"                                    . . . . . . . . . . . .
                             45 "Andiek Sumaryono"                                                      . . . . . . . . . . . .
                             46 "Andrawina"                                                             . . . . . . . . . . . .
                             47 "Antam Finance Ltd."                                                    . . . . . . . . . . . .
                             48 "Antara Surya PT"                                                       . . . . . . . . . . . .
                             49 "Anugerah Corporation"                                                  . . . . . . . . . . . .
                             50 "Anugrah Argon Medica, Pt"                                              . . . . . . . . . . . .
                             51 "Anwar & Rekan"                                                         . . . . . . . . . . . .
                             52 "APB Group Ltd."                                                        . . . . . . . . . . . .
                             53 "APP Celgar Pulp Inc."                                                  . . . . . . . . . . . .
                             54 "Aqua Group of Companies"                                               . . . . . . . . . . . .
                             55 "Arah Prana Pt"                                                         . . . . . . . . . . . .
                             56 "Arema Indonesia"                                                       . . . . . . . . . . . .
                             57 "Aria & Jonnardi"                                                       . . . . . . . . . . . .
                             58 "Aria Kanaka & Rekan"                                                   . . . . . . . . . . . .
                             59 "Aries Capital Partners Limited"                                        . . . . . . . . . . . .
                             60 "Arifin, Halid & Co"                                                    . . . . . . . . . . . .
                             61 "Arman Hendiyanto & Rekan"                                              . . . . . . . . . . . .
                             62 "Arsanesia"                                                             . . . . . . . . . . . .
                             63 "Arsitag"                                                               . . . . . . . . . . . .
                             64 "Arsyad & Rekan"                                                        . . . . . . . . . . . .
                             65 "Arte Mobile Technology Pte Ltd."                                       . . . . . . . . . . . .
                             66 "Artoncode Indonesia"                                                   . . . . . . . . . . . .
                             67 "ARTOTEL Group"                                                         . . . . . . . . . . . .
                             68 "Aruna Wijaya Sakti"                                                    . . . . . . . . . . . .
                             69 "Asata Utama Electrical Industries, Pt"                                 . . . . . . . . . . . .
                             70 "Ascott International Management Indonesia PT"                          . . . . . . . . . . . .
                             71 "ASEAN Centre for Energy."                                              . . . . . . . . . . . .
                             72 "Asep Rahmansyah & Rekan"                                               . . . . . . . . . . . .
                             73 "Asep Rianita Manshur & Suharyono"                                      . . . . . . . . . . . .
                             74 "ASHIPSHAPE Pub"                                                        . . . . . . . . . . . .
                             75 "Asia Capital Holding Limited"                                          . . . . . . . . . . . .
                             76 "Asia Color Company Limited"                                            . . . . . . . . . . . .
                             77 "Asia Minerals Corporation Limited"                                     . . . . . . . . . . . .
                             78 "Asia Petroleum Development Limited"                                    . . . . . . . . . . . .
                             79 "Asia Pulp & Paper Group PT"                                            . . . . . . . . . . . .
                             80 "Asiakomnet Multimedia, PT"                                             . . . . . . . . . . . .
                             81 "Asian Agri"                                                            . . . . . . . . . . . .
                             82 "Asian Profile Indosteel, Pt"                                           . . . . . . . . . . . .
                             83 "Asietex Sinar Indopratama, Pt"                                         . . . . . . . . . . . .
                             84 "Assegaf Hamzah & Partners"                                             . . . . . . . . . . . .
                             85 "Astec Aggregate & Mining Group"                                        . . . . . . . . . . . .
                             86 "Aster Dharma Industri"                                                 . . . . . . . . . . . .
                             87 "Asumsi.co"                                                             . . . . . . . . . . . .
                             88 "Atlantic Richfield Indonesia Inc"                                      . . . . . . . . . . . .
                             89 "Ausindo Indonesia"                                                     . . . . . . . . . . . .
                             90 "Australia Pacific Lng Project - Module Yard"                           . . . . . . . . . . . .
                             91 "AutoDirect Corporation Ltd."                                           . . . . . . . . . . . .
                             92 "AutoFleem.io"                                                          . . . . . . . . . . . .
                             93 "Awak Mas Gold Deposit, Indonesia"                                      . . . . . . . . . . . .
                             94 "AwanTunai"                                                             . . . . . . . . . . . .
                             95 "Axiomas Investment Indonesia"                                          . . . . . . . . . . . .
                             96 "AyoPay"                                                                . . . . . . . . . . . .
                             97 "Badan Kepegawaian Negara"                                              . . . . . . . . . . . .
                             98 "Badan Pengawasan Keuangan Dan Pembangunan"                             . . . . . . . . . . . .
                             99 "Badan Pengurus Daerah Asosiasi Kelompok Uppks Yogyakarta"              . . . . . . . . . . . .
                            100 "Badan Pengusahaan Kawasan Perdagangan Bebas Dan Pelabuhan Bebas Batam" . . . . . . . . . . . .
                            end



                            What am I doing here , kindly tell me. It worked just fine in the sample data previously.

                            Comment


                            • #15
                              Stata is telling you what is wrong


                              variable id does not uniquely identify the observations
                              So look for lack of uniqueness.

                              Code:
                              duplicates report f_id
                              
                              duplicates list f_id 
                               
                              One problem to watch out for is just several identifiers that are missing. This can arise from import from a spreadsheet or similar with several blank rows.

                              This FAQ is more relevant than the title implies:

                              https://www.stata.com/support/faqs/d...d-time-values/

                              Comment

                              Working...
                              X