Announcement

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

  • Error using reshape.

    Hello everyone,

    I have the following dataset:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int Year float(cocoa coffee tea)
    1960 3.0769715  4.229965  5.419287
    1961  2.452238  4.009644  5.043255
    1962 2.3037343  3.809979  4.795938
    1963   2.83032  3.661893  4.921299
    1964  2.555764 4.5401278 4.6623535
    1965 1.8284953  4.200999  5.066413
    1966  2.498808 4.0256944  4.413181
    1967  2.862174  3.836209 4.7431984
    1968 3.4799714  3.894003  4.034715
    1969 4.1133976  3.680444  3.297254
    1970  2.907652 4.4372315  3.823453
    1971 2.2035952  3.939136  3.692863
    1972  2.418084  3.953175 3.0436306
    1973  3.661782  4.003855  2.710703
    1974 4.1471906 3.6465025  3.020196
    1975 2.9806585 3.3346746  2.622963
    1976  4.834464  7.042325   2.87251
    1977  8.285694  11.04199 4.5912213
    1978  6.402001  6.434844  3.024581
    1979  5.552967  6.305437  2.750204
    1980  3.991594  5.143401  2.785964
    1981  3.184666  3.911836  2.487348
    1982  2.739556 4.3495383  2.586328
    1983  3.437219 4.5747886  3.103582
    1984  3.972359  5.160637  4.658217
    1985  3.777082  4.925164 2.6613636
    1986 3.0147066  5.489634   2.48686
    1987 2.6508186  3.159186  1.741554
    1988 1.9779524  3.191044 1.6969632
    1989 1.5584383  2.538469 1.9059527
    1990 1.5316404  1.906847 1.7972807
    1991 1.4591677 1.7981176  1.744895
    1992 1.3184253 1.4102768 2.0120442
    1993 1.2942352  1.574493  1.804751
    1994 1.6678215  3.540815 1.8767424
    1995  1.558678  3.320475 1.4161226
    1996 1.6137103   2.49454   1.57755
    1997 1.8844392  3.436564 2.3460581
    1998 2.0393958   2.92208  2.310131
    1999 1.4085815 2.3445048 2.2334988
    2000 1.1379292  1.779329 2.5484505
    2001 1.3952132 1.2926186 1.9799826
    2002  2.348558 1.3331517  1.971048
    2003 2.1995945 1.4006797 1.9392273
    2004 1.8233513 1.5100135 1.8285294
    2005 1.7538422  2.079065 1.6847206
    2006 1.7707478 2.2310436 2.1716413
    2007 2.0464036 2.4281344  1.745225
    2008 2.5069225 2.6276815 2.1571958
    2009 2.9935236  2.494976  2.610938
    2010  3.133004  3.028018  2.560042
    2011  2.684754 3.7764544  2.449512
    2012   2.17048  2.893732 2.6138964
    2013 2.2231965 2.3481514 2.1867213
    2014  2.830166 3.0685036   1.89007
    2015  3.202293  2.792256  3.028274
    2016  3.075278  2.959683 2.5759604
    2017 2.0857253  2.851354  3.052986
    2018 2.2509995 2.3529108  2.530545
    2019 2.3525846 2.2620804 2.2223198
    2020  2.420699  2.471659 2.0505192
    2021 2.2180362  2.967605 1.9303017
    2022 2.1675537  3.584937   2.21719
    end
    I would like to reshape this dataset to obtain the just three columns; Year, Products and Price. To make it clear, for each year, I would like to have three observations, cocoa, coffee and tea, with his corresponding price.

    I am trying the following:
    Code:
    reshape long cocoa coffee tea, i(Year) j(products)
    But I am obtaining the following error:

    "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. Remember this picture:"



    Anyone knows why? Thank you.

  • #2
    As documented, e.g. in the help or the manual entry, reshape long expects shared stubnames, which are prefixes (or exceptionally suffixes) shared by a bunch of variable names.

    You are feeding the command variable names.

    You can make progress by supplying the stub that reshape long needs.

    Code:
    rename (cocoa-tea) (price=)
    
    reshape long price, i(Year) j(products) string

    Comment


    • #3
      Thank you Nick Cox . I readed the manually and I did not understall well, I thought that putting the name of the variable I wanted to reshape work.

      Comment


      • #4
        That's a common guess, but it's hard to see where it comes from. Nothing in the help supports that idea. Right from the start, the explanation of reshape long is phrased in terms of stubs.

        For the advice that you may need to rename, see also https://www.stata.com/support/faqs/d...-with-reshape/

        reshape has undeservedly a bad reputation, judging by grumbles on social media, but consider Stata's point of view: it needs to know exactly what you want, including new variable names as well as existing names.

        It is not mentioned nearly so much, but stack is here an alternative, and may be easier to grasp. But it's worth persevering with reshape.

        Comment


        • #5
          Nick Cox Thank you also for the explanation! Very helpful!

          Comment

          Working...
          X