Announcement

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

  • Splitting a string variables with conditions

    Hi. In the following data, I want to do the following:

    1. split the Age variable into two variables - age and sex using the "/" as the condition in the Age variable
    2. In the remaining age variable for all ages less than a year, replace it with "1". For example, in observation 2 and 3 below, the age variable would be "1" for both
    3. split the ApptTime variable into two variables - ApptDate and ApptTime by the space in between. For example, in "30-11-2023 12:30", ApptDate == 30-11-2023 and ApptTime==12:30
    4. convert the ApptDate from a string to a date variable

    Any help would be much appreciated.


    Code:
    input str15 MRNo str8 Age str17 ApptTime
    "P1610634"     "68/F"     "30-11-2023 12:30"
    "P1602163"     "1 Mo. /M"  "30-11-2023 08:40"
    "P1602154"     "7 Mo. /M"  "30-11-2023 08:40"
    "PN1585614"    "1/F"      "30-11-2023 11:45"
    "PN1401224"    "2/M"      "30-11-2023 11:30"
    "P1609543"     "63/M"     "30-11-2023 11:25"
    "P1610326"     "46/F"     "30-11-2023 11:10"
    "P1257195"     "4/F"      "30-11-2023 10:05"
    "P1411821"     "2/M"      "30-11-2023 09:50"
    "P1539542"     "30/M"     "30-11-2023 09:45"
    "P1610610"     "69/F"     "30-11-2023 09:30"
    "N500364"      "50/M"     "30-11-2023 09:30"
    "KVC-P145026"  "57/F"     "30-11-2023 09:15"
    "P352115"      "33/F"     "30-11-2023 09:15"
    "P1541985"     "26/F"     "30-11-2023 09:00"
    "P1519669"     "21/F"     "30-11-2023 09:00"
    "P1598091"     "73/M"     "30-11-2023 09:00"
    "N492028"      "9/M"      "30-11-2023 09:00"
    "P1598574"     "67/M"     "30-11-2023 08:30"
    "N498303"      "52/F"     "29-11-2023 16:00"
    "N498303"      "52/F"     "29-11-2023 16:00"
    "N473563"      "11/M"     "29-11-2023 15:45"
    "P1345781"     "31/F"     "29-11-2023 15:30"
    "P1555159"     "26/F"     "29-11-2023 15:15"

  • #2
    See https://www.statalist.org/forums/for...variable-along for an overlapping thread with questions 1 and 2.

    Questions 3 and 4 yield to reading

    Code:
    help split 
    
    help datetime

    Comment


    • #3
      For questions 1 and 2 you have already received two useful responses to your prior post of these questions at https://www.statalist.org/forums/for...variable-along.

      For 3 and 4, a better approach is to convert the whole thing to a date-time variable (clock, in Stataspeak) and then you extract the date and time from there.
      Code:
      gen double appt_dttm = clock(ApptTime, "DMYhm")
      assert missing(appt_dttm) == missing(ApptTime)
      format appt_dttm %tc
      gen appt_date = dofc(appt_dttm)
      format appt_date %td
      gen double appt_time_of_day = appt_dttm - cofd(appt_date)
      format appt_time_of_day %tcHH:MM
      Added: Crossed with #2.

      Comment


      • #4
        Nick Cox and Clyde Schechter Thank you for your responses on both the threads. The solutions worked perfectly.

        Comment

        Working...
        X