Dear all,
I am trying to generate a loop to create dummy variables for maximum values of a variable (here income) in each group for each year. I have 10 years of data.
Firstly I wish to generate variable max_2018 showing the maximum income in all groups for day == 1 & month == 1 & year == 2018.
bysort group: egen max_2018= max(income) if day == 1 & month == 1 & year == 2018 & id != 0
There will be ties.
For that, the tie breaker is asset_to_debt ratio (variable A2D), which has to be maximum among the ties. So I create a variable maxA2D within each group showing max asset to debt ratio.
So I do:
bysort group: egen maxA2D_2018 = max(A2D) if max_2018 != .
Then I generate a dummy for when maxA2D is equal to A2D (for non-missing values) which generates our dummy for the max income with max A2D within each group.
gen D2018=1 if max_2018 == maxA2D & max_A2D !=.
replace D2018 = 0 if max_A2D == .
Question: These four commands are doing the job for one year i.e. 2018. How to generate a loop that would do this for all 10 years at once?
Thanks in advance!
Related Posts with Loop to generate dummies indicating maximum values within each group
Bayesian Nominal Response ModelPlease the entire house, I need someone to guide me on how I can fit nominal response model using ba…
Regression in data panel. Collinearity with FEHello, I am struggling with my data panel analysis. I am conducting a research with information of …
Fine and Gray calculate risksDear all, Could anyone explain how I can calculate 10-year risks of each individual in my dataset a…
Finding closest neighboursI have a survey dataset with lat/long variables of respondents' households. How can I find the 13 cl…
mcompare method in command -pwcompare-Hi I have the following puzzle Several places in the (also the pdf) help for -pwcompare-, option mc…
Subscribe to:
Post Comments (Atom)
0 Response to Loop to generate dummies indicating maximum values within each group
Post a Comment