Announcement

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

  • How to split variable that does not have spaces

    Hi Everyone,

    I would like to split (eg. Italy2019) into two variables (eg.country= Italy and year =2019).

    Since there is no space, I could not do it.

    Could someone please provide me information on how to split 'country' name and 'year' into two variable ?

    My list:

    country

    Italy2019
    Jamaica2010
    Russia2012
    .....

    Thank you.

  • #2
    Code:
    replace country = trim(country)
    gen year = substr(country, -4, 4)
    replace country = subinstr(country, year, “”, .)
    destring year, replace

    Comment


    • #3
      Here's one way.

      .ÿ
      .ÿversionÿ17.0

      .ÿ
      .ÿclearÿ*

      .ÿ
      .ÿinputÿstr25ÿcountry

      ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿcountry
      ÿÿ1.ÿItaly2019
      ÿÿ2.ÿJamaica2010
      ÿÿ3.ÿRussia2012
      ÿÿ4.ÿend

      .ÿ
      .ÿ*
      .ÿ*ÿBeginÿhere
      .ÿ*
      .ÿgenerateÿstrÿcouÿ=ÿsubstr(country,ÿ1,ÿstrlen(country)-4)

      .ÿgenerateÿintÿyeaÿ=ÿreal(substr(country,ÿ-4,ÿ4))

      .ÿlist,ÿnoobs

      ÿÿ+------------------------------+
      ÿÿ|ÿÿÿÿÿcountryÿÿÿÿÿÿÿcouÿÿÿÿyeaÿ|
      ÿÿ|------------------------------|
      ÿÿ|ÿÿÿItaly2019ÿÿÿÿÿItalyÿÿÿ2019ÿ|
      ÿÿ|ÿJamaica2010ÿÿÿJamaicaÿÿÿ2010ÿ|
      ÿÿ|ÿÿRussia2012ÿÿÿÿRussiaÿÿÿ2012ÿ|
      ÿÿ+------------------------------+

      .ÿ
      .ÿexit

      endÿofÿdo-file


      .

      Comment


      • #4
        Thank you very much.

        Comment

        Working...
        X