Announcement

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

  • Convert long to str12

    Hi,

    I have two datasets and I want to merge them using date and company_id. However, at one dataset the company_id is type long and format %9.0g and my other dataset it is type str12 and format %12s.

    Company_id variables are e.g. AEA000201011 (always a combination of letters and numbers).

    I tried to make the other variable a str12 as well using:

    tostring company_id, generate(company_id_n) format(%12.0f)

    However, for all company_id_n observations I got 1 (and Stata said: company_id_n generated as str4).

    What am I doing wrong? I think it goes wrong in the format but I am not sure.

    Thanks!

  • #2
    I am sorry, I did not get for all observations a 1. I just noticed that it counts all my unique company_ids. I want to have the same company_id before but in the same type and format as my other dataset.

    Comment


    • #3
      I've read this through a few times and still have no clear picture of your data. A verbal description doesn't convey as much as a real example and a longer verbal description is even harder to follow than a short one.

      What seems clear to me is that a numeric variable can't carry a prefix like AEA unless that is carried within a value label. If it is so carried, then tostring throws that detail away.

      Please back up and give an example of your data, as we prominently request in the FAQ Advice #12. You need to run

      Code:
      dataex date company_id
      in both datasets and show us the results.

      Comment


      • #4
        Thank you for your response. See underneath the copies. First is the one where company_id is a numeric value and second where it is a string value.

        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input float date long company_id
        16468 1
        16469 1
        16470 1
        16471 1
        16474 1
        16475 1
        16476 1
        16477 1
        16478 1
        16481 1
        16482 1
        16483 1
        16484 1
        16485 1
        16488 1
        16489 1
        16490 1
        16491 1
        16492 1
        16495 1
        16496 1
        16497 1
        16498 1
        16499 1
        16502 1
        16503 1
        16504 1
        16505 1
        16506 1
        16509 1
        16510 1
        16511 1
        16512 1
        16513 1
        16516 1
        16517 1
        16518 1
        16519 1
        16520 1
        16523 1
        16524 1
        16525 1
        16526 1
        16527 1
        16530 1
        16531 1
        16532 1
        16533 1
        16534 1
        16537 1
        16538 1
        16539 1
        16540 1
        16541 1
        16544 1
        16545 1
        16546 1
        16547 1
        16548 1
        16551 1
        16552 1
        16553 1
        16554 1
        16555 1
        16558 1
        16559 1
        16560 1
        16561 1
        16562 1
        16565 1
        16566 1
        16567 1
        16568 1
        16569 1
        16572 1
        16573 1
        16574 1
        16575 1
        16576 1
        16579 1
        16580 1
        16581 1
        16582 1
        16583 1
        16586 1
        16587 1
        16588 1
        16589 1
        16590 1
        16593 1
        16594 1
        16595 1
        16596 1
        16597 1
        16600 1
        16601 1
        16602 1
        16603 1
        16604 1
        16607 1
        end
        format %td date
        label values company_id variable
        label def variable 1 "AEA000201011", modify
        ------------------ copy up to and including the previous line ------------------


        ----------------------- copy starting from the next line -----------------------
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input int date str12 company_id
        20298 "AEA000201011"
        20663 "AEA000201011"
        20207 "AEA001501013"
        20571 "AEA001501013"
        20006 "AEA002001013"
        20355 "AEA002001013"
        20720 "AEA002001013"
        19823 "AEA002401015"
        19961 "AEA002401015"
        20327 "AEA002401015"
        20663 "AEA002401015"
        20313 "AED000201015"
        20678 "AED000201015"
        20131 "AED000901010"
        20496 "AED000901010"
        19991 "AEDFXA0M6V00"
        20355 "AEDFXA0M6V00"
        20754 "AEDFXA0M6V00"
        19102 "AEDFXMS5DFB0"
        19656 "AEDFXMS5DFB0"
        19947 "AEDFXMS5DFB0"
        20006 "AEE000301011"
        20355 "AEE000301011"
        20720 "AEE000301011"
        20496 "AEE000401019"
        20327 "AEE000501016"
        20692 "AEE000501016"
        20327 "AEE000801010"
        20692 "AEE000801010"
        20306 "AEE001501015"
        20692 "AEE001501015"
        20038 "AEF000201010"
        20369 "AEF000201010"
        20706 "AEF000201010"
        20054 "AEN000101016"
        20389 "AEN000101016"
        20720 "AEN000101016"
        20327 "AEU000401015"
        20692 "AEU000401015"
        17212 "AN8068571086"
        18032 "AN8068571086"
        18550 "AN8068571086"
        18976 "AN8068571086"
        19355 "AN8068571086"
        19709 "AN8068571086"
        20045 "AN8068571086"
        20397 "AN8068571086"
        20769 "AN8068571086"
        19779 "ANN6748L1027"
        20275 "ANN6748L1027"
        20725 "ANN6748L1027"
        20052 "ARBATE050065"
        20117 "ARBATE050065"
        20355 "ARBATE050065"
        20052 "ARBBOS050394"
        19641 "ARBRIO010194"
        20025 "ARBRIO010194"
        20433 "ARBRIO010194"
        20781 "ARBRIO010194"
        20543 "ARCAFI560015"
        19929 "ARCVZA010028"
        19641 "ARDEUT114683"
        20006 "ARDEUT114683"
        19656 "ARDEUT115466"
        19991 "ARDEUT115466"
        20355 "ARDEUT115466"
        19400 "ARESUR010035"
        19710 "ARESUR010035"
        20052 "ARESUR010035"
        20418 "ARESUR010035"
        20663 "ARESUR010035"
        20006 "ARFICR050141"
        20558 "ARFICR560032"
        20643 "ARFICR560032"
        19709 "ARGASB010027"
        20025 "ARGASB010027"
        20117 "ARGASB010027"
        20389 "ARGASB010027"
        20720 "ARGASB010027"
        19626 "ARHOLD010025"
        19961 "ARHOLD010025"
        20222 "ARHOLD010025"
        19361 "ARITAU050134"
        19807 "ARJDCC050024"
        20643 "ARMEBE560066"
        20052 "ARMERI013163"
        20418 "ARMERI013163"
        19673 "ARP125991090"
        20052 "ARP125991090"
        20433 "ARP125991090"
        20781 "ARP125991090"
        20663 "ARP2341J1058"
        19414 "ARP2787Z1000"
        19779 "ARP2787Z1000"
        20433 "ARP6558L1178"
        20692 "ARP6558L1178"
        19502 "ARP6806N1051"
        19871 "ARP6806N1051"
        20236 "ARP6806N1051"
        20601 "ARP6806N1051"
        end
        format %td date
        ------------------ copy up to and including the previous line ------------------


        With regard to the value label, you are indeed right. I see that it must be carried within a value label (dataset is created by someone using R, so I cannot tell you why this is the case)

        Comment


        • #5
          Thank you. I hope this helps. I noticed company_id indeed has a value label.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input float date long company_id
          16468 1
          16469 1
          16470 1
          end
          format %td date
          label values company_id variable
          label def variable 1 "AEA000201011", modify
          ------------------ copy up to and including the previous line ------------------



          ----------------------- copy starting from the next line -----------------------
          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input int date str12 company_id
          20298 "AEA000201011"
          20663 "AEA000201011"
          20207 "AEA001501013"
          end
          format %td date
          ------------------ copy up to and including the previous line ------------------

          Comment


          • #6
            So, the example reveals all. You need to decode, not apply tostring.

            Comment


            • #7
              Thank you! That solved indeed the problem.

              Comment

              Working...
              X