Announcement

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

  • Usijng split

    I have a date 4/25/2000 and need to divide it to three variables 4 25 and 2000. Can I do this with split and if so how.

    tyoed. edit var11

    . edit var1

    . split var1,gen(day month year)
    invalid stub day month year

    and received:

    r(198);


    Any help would be appreciated.

    Ric


  • #2
    Code:
    split var1, parse("/") gen(dmy) destring
    rename (dmy*) (day month year)
    drop dmy*

    Comment


    • #3
      Thanks Nick

      Comment


      • #4
        I should perhaps do something to deserve that after Clyde Schechter solved the problem. Perhaps the credit arises from my original contribution on split.

        Ric (Eric) has a problem with a daily date in string form. So we could call up date functions too, as in

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str9 ddate
        "4/25/2000"
        "7/1/2025" 
        end
        
        foreach f in month day year {
            gen `f' = `f'(daily(ddate, "MDY"))
        }
        
        list 
        
            +--------------------------------+
             |     ddate   month   day   year |
             |--------------------------------|
          1. | 4/25/2000       4    25   2000 |
          2. |  7/1/2025       7     1   2025 |
             +--------------------------------+
        For an introduction to Statalist, including dataex, please read https://www.statalist.org/forums/help

        For a more recent tutorial survey of dates, please read https://journals.sagepub.com/doi/pdf...6867X251341416

        Code:
        gen ndate = daily(ddate, "MDY") 
        format ndate %td
        could also be useful.

        Comment


        • #5
          Sorry Clyde I meant to thank you,. But I have another problem now. I have data on GDP per capita over time and a line of data reads:

          1871 | 2489 | 1.79%

          I want to divide the data into separate variables with year first and GDP last with the % sign off. I typed, following your suggestion as I read it:

          . split var1, parse("/") gen(yxg)
          cannot generate new variables using stub yxg
          r(110);

          What did I do wrong? Any help would be appreciated.

          Comment


          • #6
            You wrote forward slash / but meant to write pipe |

            http://www.catb.org/jargon/html/A/ASCII.html lists various names for characters.

            Comment


            • #7
              You must have a variable with stub yxg in your data. And pipe | sign is certainly a problem too.
              Code:
              . set obs 1
              number of observations (_N) was 0, now 1
              
              . generate str var1 = "1871 | 2489 | 1.79%"
              
              . split var1, parse("/") gen(yxg)
              variable created as string: 
              yxg1
              
              . split var1, parse("/") gen(yxg)
              cannot generate new variables using stub yxg
              r(110);

              Comment

              Working...
              X