Announcement

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

  • How to chanche variable date from "2022-6-7 00:00:00" to "2022-6-7"?

    How to chanche variable date from "2022-6-7 00:00:00" to "2022-6-7"?

  • #2
    Andres:
    I assume that you want to switch from string to a manageable date for further analyses:
    Code:
    set obs 1
    g var="2022-6-7 00:00:00"
    generate numdate = date(var, "YMDhms")
    format numdate %td
    list
    
         +-------------------------------+
         |               var     numdate |
         |-------------------------------|
      1. | 2022-6-7 00:00:00   07jun2022 |
         +-------------------------------+
    
    .
    Conversely, if you're aiming at obtaining a shorter -string- variable, you can go:
    Code:
    set obs 1
    g var="2022-6-7 00:00:00"
    . split var, parse("") gen(wanted)
    
    . drop wanted2
    
    . list var wanted1
    
         +------------------------------+
         |               var    wanted1 |
         |------------------------------|
      1. | 2022-6-7 00:00:00   2022-6-7 |
         +------------------------------+
    
    .
    Last edited by Carlo Lazzaro; 18 Oct 2022, 01:59.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Note on the literal reading of #1 -- replacing a string by its first word -- that you can get there directly with

      Code:
      . clear 
      
      . set obs 1
      Number of observations (_N) was 0, now 1.
      
      . gen var = "2022-6-7 00:00:00"
      
      . gen wanted = word(var, 1)
      
      . 
      . list 
      
           +------------------------------+
           |               var     wanted |
           |------------------------------|
        1. | 2022-6-7 00:00:00   2022-6-7 |
           +------------------------------+
      Note also the limit() option of split, which here would imply limit(1) as you only want the first result.

      Comment

      Working...
      X