Announcement

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

  • transmitting the value of a variable to a new variable

    Hi users,

    Considering the example in below, I wonder if it would be possible to generate a new variable (date2) and transmit the value of date to the new variable for each observation (ISIN) based on ISIN2. I am not sure which sort of commands help to get such a result as shown in table 1.

    I would be grateful if someone could help me.

    Example
    input str12(ISIN ISIN2) int date
    "GB0001771426" "GB0001771426" 20463
    "GB0001771426" "NL0009739416" 20494
    "GB0001771426" "SE0000103814" 20521
    "GB0001771426" "PLTAURN00011" 20489
    "GB0001771426"
    "GB0001771426"
    "GB0001771426"
    "GB0001771426"
    "NL0009739416"
    "NL0009739416"
    "NL0009739416"
    "NL0009739416"
    "NL0009739416"
    "NL0009739416"
    "NL0009739416"
    "NL0009739416"
    "NL0009739416"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "PLTAURN00011"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"
    "SE0000103814"

    Table1
    ISIN New variable (date2) ISIN2 Date
    GB0001771426 01/10/2016 GB0001771426 01/10/2016
    GB0001771426 01/10/2016 NL0009739416 02/10/2016
    GB0001771426 01/10/2016 SE0000103814 03/08/2016
    GB0001771426 01/10/2016 PLTAURN00011 02/05/2016
    GB0001771426 01/10/2016
    GB0001771426 01/10/2016
    GB0001771426 01/10/2016
    GB0001771426 01/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    NL0009739416 02/10/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    PLTAURN00011 02/05/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016
    SE0000103814 03/08/2016

  • #2
    This will work:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14(ISIN ISIN2) int date
    `""GB0001771426""' `""GB0001771426""' 20463
    `""GB0001771426""' `""NL0009739416""' 20494
    `""GB0001771426""' `""SE0000103814""' 20521
    `""GB0001771426""' `""PLTAURN00011""' 20489
    `""GB0001771426""' ""                     .
    `""GB0001771426""' ""                     .
    `""GB0001771426""' ""                     .
    `""GB0001771426""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""NL0009739416""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""PLTAURN00011""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    `""SE0000103814""' ""                     .
    end
    format date %td
    
    preserve
    keep ISIN2 date
    drop if missing(date)
    tempfile isin2file
    rename ISIN2 ISIN
    rename date date2
    save `isin2file'
    
    restore
    merge m:1 ISIN using `isin2file'
    order date2, after(ISIN)
    Your -input- command is not compatible with the data you show underneath it, and it was not possible to import your example from there. I don't know whether that -input- command came from a -dataex- output that got mangled through subsequent editing, or whether you just tried to write some code that more or less resembled real -dataex- output, but wasn't. Either way, you shouldn't do that. Just use -dataex- on your Stata data set: it never produces unusable results. If you didn't actually have your data imported into Stata yet, then it was premature to ask for help with code. Code and data have to fit like a hand and glove. It is a waste of time to write code for a non-existent data set: the actual data will often have features that break the code.

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

    Comment


    • #3
      Hi Clyde,
      Thanks for the help. I used dataex but the then I edited here. It was my mistake that I edited the data. The commands work well.
      Kind regards,
      Mahmoud

      Comment

      Working...
      X