Announcement

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

  • Date to string

    Hi all,

    I am finding many posts on how to convert a variable (float, string, etc.) to a properly formatted date in %td, %tm, or %tq, but I am not finding anything on how to go from the latter to the former.

    Specifically, I have dates in %tq format I wish to make string.

    How to proceed?

    Thanks in advance!

  • #2
    Guillaume:
    a trivial toy-example follows:
    Code:
    . use "https://www.stata-press.com/data/r18/nlswork.dta"
    (National Longitudinal Survey of Young Women, 14-24 years old in 1968)
    
    
    . tostring year, g(string_year)
    string_year generated as str2
    
    . list year string_year in 1/10
    
         +-----------------+
         | year   string~r |
         |-----------------|
      1. |   70         70 |
      2. |   71         71 |
      3. |   72         72 |
      4. |   73         73 |
      5. |   75         75 |
         |-----------------|
      6. |   77         77 |
      7. |   78         78 |
      8. |   80         80 |
      9. |   83         83 |
     10. |   85         85 |
         +-----------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      Originally posted by Guillaume Pousse View Post
      . . . I have dates in %tq format I wish to make string.

      How to proceed?
      You might be looking for something like this:
      Code:
      version 18
      
      clear *
      
      input int date_dt
      244
      248
      251
      end
      
      format date_dt %tq
      
      *
      * Begin here
      *
      generate str date_st = strofreal(date_dt, "%tq")
      
      list, noobs
      
      exit
      If you want to replace the variable, then you could, following Carlo's hint, do something like the following.
      Code:
      tostring date_dt, replace format("%tq") force

      Comment


      • #4
        If the variable has display format %tq, you can more directly
        Code:
        tostring varname , replace usedisplayformat

        Comment

        Working...
        X