Announcement

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

  • How to create a datetime display format that includes a curly single right quotation mark?

    I am using format in Stata 15.1 on macOS 11.5.1. I have a variable called yrmo that represents year and month. The variable is stored numerically as a monthly date. (For example, April 2021 is stored as 735.)

    I'm trying to display the variable in the following format:
    Apr ’21
    (note curly apostrophe / single right quote mark)

    I tried the following command, including an exclamation point to enable me to specify any character and then a curly quote mark:
    Code:
    format yrmo %tmMon_!’YY
    but got an error:
    Code:
    invalid %format
    r(120);
    I did not get an error when using a straight quote mark:
    Code:
    format yrmo %tmMon_!'YY
    works fine and yields Apr '21.

    Is it possible to include a curly quote mark in a datetime display format? If so, how?
    Thanks.

    P.S. Other curly quote marks such as ‘ and “ and ” also cause errors, although the straight double quote " and the backtick ` do not. Maybe it's an ASCII thing?

  • #2
    Not all characters are allowed in the date format. You can check this using the display command. If you must have an invalid character, use value labels.

    Code:
    di %tmMon_!’YY 200
    di %tmMon_!©YY 1940
    di %tmMon_!#YY 1937
    di %tmMon_!*YY 1938
    di %tmMon_!±YY 1938
    di %tmMon_!!YY 1938
    di %tmMon_!"YY 1938
    di %tmMon_!?YY 1938
    Res.:

    Code:
    . di %tmMon_!’YY 200
    invalid %format
    r(120);
    
    . 
    . di %tmMon_!©YY 1940
    invalid %format
    r(120);
    
    . 
    . di %tmMon_!#YY 1937
    Jun #21
    
    . 
    . di %tmMon_!*YY 1938
    Jul *21
    
    . 
    . di %tmMon_!±YY 1938
    invalid %format
    r(120);
    
    . 
    . di %tmMon_!!YY 1938
    Jul !21
    
    . 
    . di %tmMon_!"YY 1938
    Jul "21
    
    . 
    . di %tmMon_!?YY 1938
    Jul ?21

    Comment


    • #3
      Thank you very much for the info and the suggestion.

      Comment


      • #4
        Here's one approach, based on labmask from the Stata Journal.


        Code:
        webuse grunfeld, clear
        gen year2 = char(39) + strofreal(mod(year, 100))
        labmask year, values(year2)
        line invest year, xla(1935/1954, valuelabel) c(L) ysc(log)

        Comment


        • #5
          Tristan Fabriani -

          Building on Andrew's exploration, I hypothesize that when Stata implemented system-wide Unicode support a few releases ago, they overlooked the insertion of arbitrary characters into format codes, and as a consequence, this still supports only one-byte characters - the first 127 code points. In other words, as you surmised in post #1, it is indeed an ASCII thing. But the documentation does not seem to express this limitation, and I'm inclined to think it's an oversight. Certainly it's not in the spirit of the bulk of Stata's Unicode support.

          While it doesn't help your short-term problem, I'd suggest reporting this to Stata Technical Services.

          An alternative to using value labels is to construct a string variable with the formatted date, and then substitute in the desired Unicode character, and use the string rather than numeric date for display purposes. This is imperfect but it may meet your needs.
          Code:
          . set obs 1
          Number of observations (_N) was 0, now 1.
          
          . generate yrmo = 735
          
          . format yrmo %tm
          
          . generate yrmos = strofreal(yrmo,"%tmMon_YY")
          
          . replace yrmos = usubinstr(yrmos," "," ’",.)
          variable yrmos was str6 now str9
          (1 real change made)
          
          . list, clean noobs
          
                yrmo     yrmos  
              2021m4   Apr ’21

          Comment

          Working...
          X