Hi everyone,
I am trying to expand a dataset that looks like this:
Dataset A
| contract_id |
region_id |
county_n |
| 1 |
1 |
5 |
| 2 |
1 |
5 |
| 3 |
2 |
4 |
| 4 |
2 |
4 |
| 5 |
3 |
3 |
| 6 |
3 |
3 |
In brief, this dataset includes unique contract_ids and each contract serves a region made up of countries (county_n represents the number of counties in a region. I am trying to expand this dataset so it has 1 row for every contract_id and county_id by merging it with this dataset:
Dataset B
| region_id |
county_id |
| 1 |
11 |
| 1 |
12 |
| 1 |
13 |
| 1 |
14 |
| 1 |
15 |
| 2 |
22 |
| 2 |
23 |
| 2 |
24 |
| 2 |
25 |
| 2 |
26 |
| 3 |
35 |
| 3 |
36 |
| 3 |
37 |
| 3 |
38 |
| 3 |
39 |
I've tried expanding Dataset A by the number of counties served by each region and then merging w/ Dataset B:
Code:
levelsof county_n, local(levels)
foreach n of local levels {
expand `n' if county_n == `n'
merge m:1 region_id using data_B,replace update
drop if _merge==2
drop _merge
}
However, this is inappropriate because it doesn't grab every county_id in Dataset B.
Any advice would be appreciated!
0 Response to Expanding Dataset
Post a Comment