Announcement

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

  • keep just a certain number of characters

    Dear all,

    I have two variables and iI want to keep just the year of each.

    (1) a long variables mdate of type "28mar1996". How can I keep just "1996"?
    (2) a float variable fdate of type "1996m3".

    Could anyone of you please guide so that I generate a new variable "year" where I have just 1996?

    My attemp was: gen str4 year = substr(string(mdate), 1, 4) --> however here what I get instead of 1996 is 434.

    Thank you in advance for any piece of advice!

  • #2
    Stata date and time variables are complicated.

    (1) a long variables mdate of type "28mar1996". How can I keep just "1996"?
    (2) a float variable fdate of type "1996m3".
    The best I can say is that, in Stata terminology, neither of these things exist. Point being that this description gives no clear idea what you actually have. Suggested solutions based on educated guesses of what your variables really might be have only a low probability of working.

    Please post back using the -dataex- command to show actual example data from your Stata data set. Once we have that in hand, I'm pretty sure solving your problem will not be hard.

    If you are running version 18, 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.
    Last edited by Clyde Schechter; 25 May 2023, 11:09.

    Comment


    • #3
      Dear Mr. Schechter,

      This is what I get when typing "dataex fdate mdate"

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input long fdate float mdate
      13236 434
      13592 446
      14326 470
      15060 494
      15791 518
      16162 531
      16526 542
      16883 554
      17234 566
      17603 578
      17969 590
      18336 602
      13006 427
      15578 511
      15935 523
      16274 534
      16639 546
      16999 558
      17367 570
      17724 582
      18094 594
      18459 606
      18821 618
      19193 630
      19565 642
      19921 654
      20284 666
      20648 678
      21376 702
      21748 714
      12929 424
      13298 436
      13662 448
      12852 422
      13584 446
      13947 458
      14312 470
      15021 493
      15755 517
      16126 529
      16497 542
      16854 553
      17220 565
      17581 577
      17948 589
      18312 601
      18676 613
      19044 625
      19404 637
      19775 649
      20146 661
      20503 673
      21602 709
      18000 591
      18396 604
      14077 462
      14453 474
      15174 498
      15544 510
      15915 522
      16280 534
      16642 546
      17013 558
      17366 570
      17743 582
      18106 594
      18471 606
      18837 618
      13240 435
      13604 446
      13970 459
      14335 471
      14699 482
      13509 443
      13879 455
      18002 591
      18367 603
      18695 614
      19099 627
      19463 639
      19814 651
      20193 663
      20557 675
      21608 709
      22004 722
      13254 435
      13692 449
      13237 434
      13604 446
      13983 459
      14334 470
      14717 483
      12865 422
      13219 434
      15421 506
      15788 518
      13053 428
      13419 440
      13786 452
      14151 464
      end
      format %td fdate
      format %tm mdate
      Attached Files

      Comment


      • #4
        This is what I get when typing "dataex fdate mdate"
        Perfect! Thank you.

        Code:
        gen fdate_year = year(fdate)
        gen mdate_year = year(dofm(mdate))
        will give you what you want.

        In your example, it is the case that mdate is always just the month and year of the full date in fdate. So, if this is true in the entire data set, you don't really need two separate year variables, as they will always be the same.

        Suggested reading: -help datetime-, to get an understanding of how Stata represents dates and times internally, how to control the way in which they are displayed, how to convert among different types of date/time variables, and how to create them. It's a lengthy read, and you will not remember it all on first reading. But it will give you an overview of what is going on, and you can always later refer back to the help files for unremembered syntax details when you have to actually make use of this knowledge. After some experience, the kinds of date/time variables and transformations that come up commonly in your workflow will sink in and you will become less dependent on the help file.

        Comment


        • #5
          thank you very much!!!

          Comment

          Working...
          X