Announcement

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

  • Renaming date variables

    Hello gang

    I'm trying to fix such that my date variable, specifically the one showing month, looks a little "cleaner". Initially I had a date variable that showed the date that an insurance customer set up an insurance offer: "date_insurance_offer". I then used this to make two new variables showing the dates in months and quarters by writing the code:

    generate insurance_offer_month = mofd(date_insurance_offer)
    format insurance_offer_month %tm
    generate insurance_offer_quarter = qofd(date_insurance_offer)
    format insurance_offer_quarter %tq

    Now the variable showing the quarters is fine, but the variable showing the months is showing 2020m1 for january 2020, 2020m2 for february 2020 etc. What i wanted to do is to make the variable "insurance_offer_month" show the months as they are named. For example 2020m1 would preferably be 2020jan, 2020m2 would be 2020feb etc, or something similar.

    Is this possible somehow?

    Appreciate any help!
    Last edited by Tor Haug Anonsen; 31 Oct 2021, 05:41.

  • #2
    Renaming is not what you want. You want a different display format.

    Code:
    help datetime display formats
    
    
    . di %tmCCYYmon ym(2020, 1)
    2020jan

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Renaming is not what you want. You want a different display format.

      Code:
      help datetime display formats
      
      
      . di %tmCCYYmon ym(2020, 1)
      2020jan
      Thank you, Nick. That worked! And also thank you for the "groups" module. It displays my data a lot better than tabulate does most of the time.

      Comment


      • #4
        Thanks for the thanks, but groups (Stata Journal) doesn't do anything special about date variables, so if there is any credit for that it belongs to list.

        Comment


        • #5
          No, I know. This was just because i saw you wrote a very good description of "groups", which i have used quite a lot.

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Thanks for the thanks, but groups (Stata Journal) doesn't do anything special about date variables, so if there is any credit for that it belongs to list.
            Hi again

            Thought I would just ask on this post instead of making a new one;
            I was wondering if there is any options in the "groups" code to make the variable name appear in its full name/size. Any variable name that is over 6 characters just get a "~x" where "x" represents the last character of the variable name (as shown below). I know that there are options like "varwidth" that would solve this with other types of descriptive codes, but it is not an option for "groups".

            Do you have any suggestions?


            Click image for larger version

Name:	SS10 STATA.PNG
Views:	1
Size:	2.4 KB
ID:	1634525

            Comment


            • #7
              #6 should have been the start of a new thread -- it's nothing to do with the thread title -- but groups allows list options to meet this need:

              Code:
              . sysuse auto, clear
              (1978 automobile data)
              
              . rename foreign foreignmeansoutsideUS
              
              . groups foreign
              
                +-------------------------------------+
                | foreig~S   Freq.   Percent      %<= |
                |-------------------------------------|
                | Domestic      52     70.27    70.27 |
                |  Foreign      22     29.73   100.00 |
                +-------------------------------------+
              .
              . groups foreign, abbrev(21)
              
                +--------------------------------------------------+
                | foreignmeansoutsideUS   Freq.   Percent      %<= |
                |--------------------------------------------------|
                |              Domestic      52     70.27    70.27 |
                |               Foreign      22     29.73   100.00 |
                +--------------------------------------------------+

              Comment


              • #8
                Originally posted by Nick Cox View Post
                #6 should have been the start of a new thread -- it's nothing to do with the thread title -- but groups allows list options to meet this need:

                Code:
                . sysuse auto, clear
                (1978 automobile data)
                
                . rename foreign foreignmeansoutsideUS
                
                . groups foreign
                
                +-------------------------------------+
                | foreig~S Freq. Percent %<= |
                |-------------------------------------|
                | Domestic 52 70.27 70.27 |
                | Foreign 22 29.73 100.00 |
                +-------------------------------------+
                .
                . groups foreign, abbrev(21)
                
                +--------------------------------------------------+
                | foreignmeansoutsideUS Freq. Percent %<= |
                |--------------------------------------------------|
                | Domestic 52 70.27 70.27 |
                | Foreign 22 29.73 100.00 |
                +--------------------------------------------------+
                For some reason i did not get a notification for this. Thank you for the answer, this worked perfectly!

                Comment


                • #9
                  Thanks for the thanks. You' would get an email notification of an answer if and only if you had subscribed to the thread (or to everything).

                  Comment

                  Working...
                  X