Announcement

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

  • How can I convert Variable names into the value of a new variable - in Stata?

    Dear All,


    I have a dataset that looks like this:

    year a b c ...
    2000 value value value
    2001 value value value
    2002 value value value
    I would like to convert the variable names a, b, c into values of a new ID variable, for then being able to declare it as a panel.

    The actual number of rows and columns is very large.

    I have beeen reading through reshaping help but cant seem to make it work.

    Any help would be very much appreciated.

  • #2
    Code:
    rename (a-c) var=
    quietly reshape long var, i(year) j(new_id) string
    See illustration below.

    .ÿ
    .ÿversionÿ16.1

    .ÿ
    .ÿclearÿ*

    .ÿ
    .ÿquietlyÿinputÿintÿyearÿstr244(aÿbÿc)

    .ÿ
    .ÿ*
    .ÿ*ÿBeginÿhere
    .ÿ*
    .ÿrenameÿ(a-c)ÿvar=

    .ÿquietlyÿreshapeÿlongÿvar,ÿi(year)ÿj(new_id)ÿstring

    .ÿ
    .ÿlist,ÿnoobsÿsepby(year)

    ÿÿ+-----------------------+
    ÿÿ|ÿyearÿÿÿnew_idÿÿÿÿÿvarÿ|
    ÿÿ|-----------------------|
    ÿÿ|ÿ2000ÿÿÿÿÿÿÿÿaÿÿÿvalueÿ|
    ÿÿ|ÿ2000ÿÿÿÿÿÿÿÿbÿÿÿvalueÿ|
    ÿÿ|ÿ2000ÿÿÿÿÿÿÿÿcÿÿÿvalueÿ|
    ÿÿ|-----------------------|
    ÿÿ|ÿ2001ÿÿÿÿÿÿÿÿaÿÿÿvalueÿ|
    ÿÿ|ÿ2001ÿÿÿÿÿÿÿÿbÿÿÿvalueÿ|
    ÿÿ|ÿ2001ÿÿÿÿÿÿÿÿcÿÿÿvalueÿ|
    ÿÿ|-----------------------|
    ÿÿ|ÿ2002ÿÿÿÿÿÿÿÿaÿÿÿvalueÿ|
    ÿÿ|ÿ2002ÿÿÿÿÿÿÿÿbÿÿÿvalueÿ|
    ÿÿ|ÿ2002ÿÿÿÿÿÿÿÿcÿÿÿvalueÿ|
    ÿÿ+-----------------------+

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .
    Code:
    help rename group

    Comment


    • #3
      I can think of two reasons you are having trouble getting -reshape- to work. One is that it is a difficult command to learn; it takes a lot of practice before, suddenly, it "clicks" in your head and you just immediately see how to use it for whatever you need.

      The other reason is that, if your variables are really named a, b, and c (which I doubt) you have to change their names to something systematic before you can apply -reshape-. It may well be true of your real variable names as well.

      Code:
      rename (a b c) value=
      reshape long value, i(year) j(ID) string
      Now, you say the number of variables (columns) is very large. It may be too large to use the -rename- command I suggested. Perhaps you can resolve that by using wildcards. If not, there is another approach. Let me imagine that you have a large number of variables like a, b, and c, and that there is also a small number of other variables (maybe even none at all) that should not go into the ID variable. Then you can do this:

      Code:
      local not_for_ID list the variables that won't go into ID here
      ds year `not_for_ID', not
      rename (`r(varlist)') value=
      reshape long value, i(year) j(ID) string
      Now another problem you might encounter is if some of the variable names in question are too long to allow the addition of value in front of them. In that case, instead of value, use just v. If any of the variable names are too long to even allow adding a single character, you'll have to separately first give those shorter names.

      In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 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.

      Comment

      Working...
      X