Announcement

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

  • Reshape Wide Issue

    Hi, I have been trying to reshape my data from long to wide format using reshape wide. The data looks like this:

    OrganisationName AssetClass CapitalCommitted TotalCapitalCommitted
    Maroni Private Equity 50 120
    Susani Private Equity 70 120
    Mazia Debt 30 75
    Mungo Debt 45 75

    Total Capital Committed denotes the total amount of capital committed per asset class by all organisations (I obtained this number by using the command: egen TotalCapitalCommitted = total(CapitalCommitted), by(AssetClass).

    I now would like to reshape my data from long to wide format and I have been trying to do so through: reshape wide TotalCapitalCommitted, i(OrganisationName) j(AssetClass) string, however whenever I run this code I get the error message: TotalCapitalCommittedInfrastructure invalid variable name. Would anyone be able to help me? The most important piece of information I have to showcase is the total capital committed per asset class.
    Many thanks

  • #2
    There is nothing wrong with your code. The problem is that your data is not quite suitable for this task. TotalCapitalCommittedInfrastructure is a variable your -reshape- command asks Stata to create by combining TotalCapitalCommited with a value of AssetClass = Infrastructure. The problem is that variable names cannot exceed 32 characters, and TotalCapitalCommittedInfrastructure has 35. You can either rename TotalCapitalCommitted to something shorter, or you can -replace AssetClass = "something_no_longer_than_11_characters" if AssetClass == "Infrastructure"-. Whichever way you go, you must do it before you -reshape wide-. Also, if you choose to rename TotalCapitalCommitted, you must, evidently, change that in the -reshape wide- command as well.

    While you are doing that, you should probably do something like this:

    Code:
    levelsof AssetClass if strlen(AssetClass) > 11
    This will show you if there are any other values of AssetClass that are too long and will leave you with the same problem. Other pitfalls to watch out for when going to wide layout: the values of AssetClass, in order to be part of a valid variable name, can contain only letters, numbers, and underscore (_) characters. So be on the look out for blanks (embedded within, or, harder to spot, padding the left or right end) or special characters which are commonly found in text variables (such as punctuation marks, or *, or hyphen or comma) and are illegal in Stata variable names.

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

    When asking for help with code, always show example data. When showing example data, always use -dataex-.
    Last edited by Clyde Schechter; 31 Oct 2021, 16:40. Reason: Correct several typos.

    Comment

    Working...
    X