Quantcast
Channel: Statalist
Viewing all articles
Browse latest Browse all 73868

Generate a variable from one dataset and save it into a different dataset

$
0
0
Hi all,

I am trying to combine some data from one dataset and save it into a different one. The variables I have in the first dataset are "id" which describes general groups, "point", and "id2" which numbers the observations within groups.
What I want to do is a new dataset with variable "id" (taken from the original dataset) and "route" (crated by combining all points within a group).
For example if for id==1 we have a subset point={A, B, C} then I would like to have in the new dataset route={A-B, A-C, B-C} for id==1.

Here is an example of what the original database would look like:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int id str3 point int id2
1 "A" 1
1 "B" 2
1 "C" 3
1 "D" 4
2 "B" 1
2 "D" 2
2 "E" 3
3 "A" 1
3 "C" 2
end
And here is an example of what I want the new file to look like:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input int id str4 route
1 "A-B"
1 "A-C"
1 "A-D"
1 "B-C"
1 "B-D"
1 "C-D"
2 "B-D"
2 "B-E"
2 "D-E"
3 "A-C"
end
I was thinking about creating a .do file like the following:

summarize id
local max1=r(max)
forvalues i=1/`max1' {
summarize id2 if id==i
local max2=r(max)
forvalues j=1/`max2'-1 {
forvalues k=`j'/`max2' {
[This is where I would like to combine "point" j with "point j+1,...,max2 and save that data in a new file]
}
}
}

I would really appreciate any help related to completing the code written above or some different way to do it.

Thanks,

Alonso











Viewing all articles
Browse latest Browse all 73868

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>