Ilkin
Ilkin

Reputation: 1

How to split a string variable and add its values in separate rows

I want to add multiple rows by deriving them from a string column in Stata.

I have a dataset like the following one:

year    countryname        intensitylevel
1990    India, Pakistan    1
1991    India, Pakistan    1
1992    India, Pakistan    1
1996    India, Pakistan    1

To be more precise, I want to split the country name variable for each country separately.

In the end, I want to have a dataset like the one below:

year    countryname        intensitylevel
1990    India              1
1990    Pakistan           1
1991    India              1
1991    Pakistan           1 

Upvotes: 0

Views: 1550

Answers (1)

user8682794
user8682794

Reputation:

This is a simple split and reshape:

clear

input year str15 countryname intensitylevel
1990    "India, Pakistan"    1
1991    "India, Pakistan"    1
1992    "India, Pakistan"    1
1996    "India, Pakistan"    1
end

split countryname, p(,)
drop countryname
reshape long countryname, i(countryname* year)

sort year countryname
list year countryname intensitylevel, abbreviate(15) sepby(year)

     +-------------------------------------+
     | year   countryname   intensitylevel |
     |-------------------------------------|
  1. | 1990      Pakistan                1 |
  2. | 1990         India                1 |
     |-------------------------------------|
  3. | 1991      Pakistan                1 |
  4. | 1991         India                1 |
     |-------------------------------------|
  5. | 1992      Pakistan                1 |
  6. | 1992         India                1 |
     |-------------------------------------|
  7. | 1996      Pakistan                1 |
  8. | 1996         India                1 |
     +-------------------------------------+

Upvotes: 1

Related Questions