Announcement

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

  • Generating a dummy variable to measure change in another

    Hi there,

    I've come stuck with how to generate a dummy variable that indicates where a bank has changed clusters. As you can see from the snap off my sample, I have the institutionname, year, and cluster the bank belongs to each year.

    I'd like to be able to generate a variable that indicates 0 if a bank hasn't changed cluster value (_clus_7) on the previous year, and 1 if it has. On the photo attached it would look like a 1 for observation 6 and 0 for everything else.

    As you can seen, I've generated a variable 'changed' to indicate if the bank has changed at all at any point between 2010-2016 using this command:

    by institutionname (_clus_7), sort: gen changed = (_clus_7[1] != _clus_7[_N])

    but this isn't precise enough as I need to know what year that change occurred in to work if there is a significant change in banking practice after the EU's Banking Union was introduced.

    Is it also possible to be even more precise and generate dummy variables with a value of 1 if a bank changed from cluster value of 1 to 4 say in a given year so I am able to track precisely which banks are going to which clusters from where?

    Many thanks for your insight,

    Olly






    Attached Files

  • #2
    So something like this:

    Code:
    by institutionname (year), sort: gen changed_from = _clus_7]_n-1] ///
        if _n > 1 & _clus_7[_n-1] != _clus_7
    by institutionname (year): gen changed_to = _clus_7 if !missing(changed_from)
    gen change = "No Change" if missing(changed_from)
    replace change = string(changed_from) + " to " + string(changed_to)
    Note: Because you did not provide your example data in a usable form (data from screenshots can't be imported to Stata), this code is not tested and may contain errors.

    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, it 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-.

    And please read the Forum FAQ for excellent advice about the most effective and helpful ways to post example data, code, and Stata output.


    Comment


    • #3
      Hi there Clyde,

      Your suggested code worked beautifully, thank you very much! I just wanted to know if you knew what the appropriate code would be if I just wanted to create a simple dummy variable for each bank of 0 for all years if a bank hasn't changed cluster at all in any of the years, and 1 for each bank for all years if it has changed cluster in any one of those years?

      Many thanks

      Olly

      Comment


      • #4
        FAQ https://www.stata.com/support/faqs/d...ions-in-group/

        Our own Statalist FAQ does advise looking through the Stata FAQs before asking a question.

        Comment

        Working...
        X