Good day everybody,

I am quite new to Stata and I have a question regarding creating a new variable based on some conditions. I hope you guys could help me with this, please. Below is an example of my current panel dataset. In this dataset, surveyees can list down up to 7 jobs (i.e., job_1, job_2,..., job_7) if it is applicable for them. For each job they fill in, they also need to write down the working hours as well. The objective that I want to achieve is to create a variable "main job" for them (i.e., the last column).

For a person who has only one job (such as the observation id 1-year 2020), that job is of course the main job. If a person has more than 1 job, the job that the person spends more time with is considered as the main job. For example, the observation with id 3-year 2020, he works 2 jobs as in healthcare and waiter. The working hours for healthcare is 43, which is greater than the working hours of waiting, therefore, his main job is healthcare. There are some cases that in which the amount of hours between 2 jobs is equal (for example the observation id 2-year 2021), both of her jobs take 10 hours. In these cases, the first job that she reports is assumed to be the main job. For example, the observation id 2-year 2021, being secretary and manager take her 10 hours each. Since secretary "comes first"; thus, secretary is the main job.

Here is the summary of the conditions for a job to be the main job:
1) If a person only has one job, that job is the main job.
2) If a person has more than one job, the job with the highest working hours is the main job.
3) If a person has multiple jobs, and they all have the same working hours, the job "comes first" is the main job.
id year job_1 job_2 job_3 job_4 job_5 job_6 job_7 wkhrs1 wkhrs2 wkhrs3 wkhrs4 wkhrs5 wkhr6 wkhrs7 main job
1 2020 accounting 15
1 2021 accounting audit 15 30
2 2020 manager 45
2 2021 secretary manager 10 10
3 2020 healthcare waiter 43 5
It would be so great if you guys could help me with this. Thank you so much in advance.