stata - How to find maximum distance apart of values within a variable -


i create working example dataset:

input /// group value 1    3 1    2 1    3  2    4 2    6 2    7  3    4 3    4  3    4 3    4 4    17 4    2 5    3 5    5 5    12 end 

my goal figure out maximum distance between incremental values within group. group 2, 2, because next highest value after 4 6. note value relevant 4 6, not 7, because 7 not next highest value after 4. result group 3 0 because there 1 value in group 3. there 1 result per group.

what want get:

input /// group value result 1    3    1 1    2    1 1    3    1 2    4    2 2    6    2 2    7    2 3    4    0 3    4    0 3    4    0 3    4    0 4    17   15 4    2    15 5    3    7 5    5    7 5    12   7 end 

the order not important, order above can change no problem.

any tips?

i may have figured out:

bys group (value): gen d = value[_n+1] - value[_n] bys group: egen result = max(d) drop d 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -