Announcement

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

  • Changing/Editing a formatted "date" variable on STATA

    I need to edit a few observations of "date" variables using the STATA browser/edit option. The date variable is already formatted using the necessary syntax. Is there a way I can edit/change these selected observations (from date to MM/DD/YYYY? The date of these observations has been wrongly written as DD/MM/YYYY. I will be grateful to receive teh advice. Thanks

  • #2
    Do not manually edit the data. If it is a matter of simply changing the display format, then use the command highlighted below.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float date
    23620
    end
    format %tdD/N/Y date
    
    list
    format date %tdN/D/Y 
    list
    Res.:

    Code:
    . format %tdD/N/Y date
    
    .
    .
    .
    . list
    
         +----------+
         |     date |
         |----------|
      1. | 01/09/24 |
         +----------+
    
    .
    . format date %tdN/D/Y
    
    .
    . list
    
         +----------+
         |     date |
         |----------|
      1. | 09/01/24 |
         +----------+
    Code:
    
    

    Comment


    • #3
      If #1 corresponds to below where some dates are incorrect, then you can tag the incorrect dates and do the following:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input float(date tag)
      23620 0
      23621 0
      23444 1
      23623 0
      23624 0
      23536 1
      23626 0
      end
      format %tdN/D/Y date
      
      *INCORRECT DATES (tag==1)
      list, sep(0)
      
      replace date= mdy(day(date), month(date), year(date)) if tag
      list, sep(0)
      Res.:

      Code:
      . *INCORRECT DATES (tag==1)
      
      . 
      . list, sep(0)
      
           +----------------+
           |     date   tag |
           |----------------|
        1. | 09/01/24     0 |
        2. | 09/02/24     0 |
        3. | 03/09/24     1 |
        4. | 09/04/24     0 |
        5. | 09/05/24     0 |
        6. | 06/09/24     1 |
        7. | 09/07/24     0 |
           +----------------+
      
      . 
      . 
      . 
      . replace date= mdy(day(date), month(date), year(date)) if tag
      (2 real changes made)
      
      . 
      . list, sep(0)
      
           +----------------+
           |     date   tag |
           |----------------|
        1. | 09/01/24     0 |
        2. | 09/02/24     0 |
        3. | 09/03/24     1 |
        4. | 09/04/24     0 |
        5. | 09/05/24     0 |
        6. | 09/06/24     1 |
        7. | 09/07/24     0 |
           +----------------+

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        If #1 corresponds to below where some dates are incorrect, then you can tag the incorrect dates and do the following:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input float(date tag)
        23620 0
        23621 0
        23444 1
        23623 0
        23624 0
        23536 1
        23626 0
        end
        format %tdN/D/Y date
        
        *INCORRECT DATES (tag==1)
        list, sep(0)
        
        replace date= mdy(day(date), month(date), year(date)) if tag
        list, sep(0)
        Res.:

        Code:
        . *INCORRECT DATES (tag==1)
        
        .
        . list, sep(0)
        
        +----------------+
        | date tag |
        |----------------|
        1. | 09/01/24 0 |
        2. | 09/02/24 0 |
        3. | 03/09/24 1 |
        4. | 09/04/24 0 |
        5. | 09/05/24 0 |
        6. | 06/09/24 1 |
        7. | 09/07/24 0 |
        +----------------+
        
        .
        .
        .
        . replace date= mdy(day(date), month(date), year(date)) if tag
        (2 real changes made)
        
        .
        . list, sep(0)
        
        +----------------+
        | date tag |
        |----------------|
        1. | 09/01/24 0 |
        2. | 09/02/24 0 |
        3. | 09/03/24 1 |
        4. | 09/04/24 0 |
        5. | 09/05/24 0 |
        6. | 09/06/24 1 |
        7. | 09/07/24 0 |
        +----------------+
        Thank you. Very helpful

        Comment

        Working...
        X