Hello,
I've encountered a peculiar issue when I tried to use "_n==1" within an egen command and while using a by group.
The data is structured this way: each observation is a job with various variables that describe it. A single person (id) can hold several jobs. The goal is to assign an occupational code of the highest paying job to all of persons's observations.
This is the code I tried first:
It didn't work correctly and only assigned the expected values to a single id, seemingly refusing to hop to other id's besides the first one.
This code worked correctly and produced what I expected:
So the question is why didn't the first version of the code work correctly despite seemingly having identical logic to the second version?
Here's the code for data, it includes the resulting variables of both versions of the code:
Thanks.
I've encountered a peculiar issue when I tried to use "_n==1" within an egen command and while using a by group.
The data is structured this way: each observation is a job with various variables that describe it. A single person (id) can hold several jobs. The goal is to assign an occupational code of the highest paying job to all of persons's observations.
This is the code I tried first:
Code:
gsort id -wage by id: egen main_occ_1=max(occ/(_n==1))
This code worked correctly and produced what I expected:
Code:
gsort id -wage by id: g first=_n==1 by id: egen main_occ_2=max(occ/(first==1))
Here's the code for data, it includes the resulting variables of both versions of the code:
Code:
clear input float(id wage occupation_code main_occ first main_occ_2) 1 5000 9 9 1 9 1 3000 4 9 0 9 1 2000 8 9 0 9 1 1000 5 9 0 9 2 3000 2 . 1 2 2 2000 7 . 0 2 2 2000 1 . 0 2 3 4000 5 . 1 5 3 1000 8 . 0 5 end
Thanks.
Comment