I would like to create a variable named region in one column. This is an example of my dataset:
| id | issue | ind | scopes_text |
| 20181213_002 | ADVOCACY_AND_MEDIA | A2 | Local |
| CAPACITY_DEVELOPMENT | A3 | ||
| CHILDREN_AND_YOUTH | |||
| 20190114_001 | ADVOCACY_AND_MEDIA | A1 | Global |
| A2 | |||
| 20190131_001 | ADVOCACY_AND_MEDIA | F1 | Regional - Americas, Oceania |
| CAPACITY_DEVELOPMENT | F2 | ||
| F3 | |||
| 20190131_002 | CAPACITY_DEVELOPMENT | B2 | Regional - Africa, Asia, Oceania |
| CHILDREN_AND_YOUTH | B4 | ||
I applied the following code to extract the regions from scopes_text:
Code:
ge wherenum = .
quietly forval j = 0/9 {
replace wherenum = min(wherenum, strpos(scopes_text, "`j'")) if strpos(scopes_text, "`j'")
}
gen scope, parse(- ,)
Then, I obtained the following:
| id | issue | ind | scopes_text | scope1 | scope2 | scope3 | scope4 |
| 20181213_002 | ADVOCACY_AND_MEDIA | A2 | Local | Local | |||
| CAPACITY_DEVELOPMENT | A3 | ||||||
| CHILDREN_AND_YOUTH | |||||||
| 20190114_001 | ADVOCACY_AND_MEDIA | A1 | Global | Global | |||
| A2 | |||||||
| 20190131_001 | ADVOCACY_AND_MEDIA | F1 | Regional - Americas, Oceania | Regional | Americas | Oceania | |
| CAPACITY_DEVELOPMENT | F2 | ||||||
| F3 | |||||||
| 20190131_002 | CAPACITY_DEVELOPMENT | B2 | Regional - Africa, Asia, Oceania | Regional | Africa | Asia | Oceania |
| CHILDREN_AND_YOUTH | B4 | ||||||
I would like to create a variable named region (in one column) containing scope from 2 to N as below (it is important that I can accommodate N number of regions). How could I create this variable region?Thank you in advance for your orientation!
| id | issue | ind | scopes_text | scope1 | scope2 | scope3 | scope4 | region |
| 20181213_002 | ADVOCACY_AND_MEDIA | A2 | Local | Local | ||||
| CAPACITY_DEVELOPMENT | A3 | |||||||
| CHILDREN_AND_YOUTH | ||||||||
| 20190114_001 | ADVOCACY_AND_MEDIA | A1 | Global | Global | ||||
| A2 | ||||||||
| 20190131_001 | ADVOCACY_AND_MEDIA | F1 | Regional - Americas, Oceania | Regional | Americas | Oceania | Americas | |
| CAPACITY_DEVELOPMENT | F2 | Oceania | ||||||
| F3 | ||||||||
| 20190131_002 | CAPACITY_DEVELOPMENT | B2 | Regional - Africa, Asia, Oceania | Regional | Africa | Asia | Oceania | Africa |
| CHILDREN_AND_YOUTH | B4 | Asia | ||||||
| Oceania | ||||||||
0 Response to Reshape only a group of variables within a dataset
Post a Comment