Hi all,
I've run into some unexpected egen behavior that broke a function that I commonly use in my code. I've boiled it down to the following example:
Test #1 Code:
Test #1 Output:
Test #2 Code:
Test #2 Output:
As you can see, the only difference between the two code snippets is that the first performs a collapse and the second does not. In the output, however, you'll see that egen preserves the sort order in the second output but not the first.
Maybe I never should have expected egen to preserve sort order, but to me this seems like odd behavior. I'm running the 31 Mar 2020 build of Stata 16.1 MP on MacOS. Please let me know if anyone has any thoughts on this!
Thanks,
Reed
I've run into some unexpected egen behavior that broke a function that I commonly use in my code. I've boiled it down to the following example:
Test #1 Code:
Code:
/* synthesize */ clear set obs 2 generate str = cond(_n == 1, "one", "two") generate num = _n /* collapse */ collapse num, by(str) /* average */ set obs 3 egen avg = mean(num)
Code:
+-----------------+ | str num avg | |-----------------| 1. | . 1.5 | 2. | one 1 1.5 | 3. | two 2 1.5 | +-----------------+
Code:
/* synthesize */ clear set obs 2 generate str = cond(_n == 1, "one", "two") generate num = _n /* DO NOT collapse */ //collapse num, by(str) /* average */ set obs 3 egen avg = mean(num)
Code:
+-----------------+ | str num avg | |-----------------| 1. | one 1 1.5 | 2. | two 2 1.5 | 3. | . 1.5 | +-----------------+
Maybe I never should have expected egen to preserve sort order, but to me this seems like odd behavior. I'm running the 31 Mar 2020 build of Stata 16.1 MP on MacOS. Please let me know if anyone has any thoughts on this!
Thanks,
Reed
Comment