Hi all,

As part of my master's progam in Accountancy, I am doing a replication study. I was wondering if anyone could help me with the following question.

Let me explain my situation: my data exists of financial data on S&P 500 firms during multiple years. I divided the observations in groups (group2_sic2) of firms with the same year and the same industry code (sic2), unless the group would exist of less than 8 firms. In that case, the observations are allocated to a year-specific "other" group (i.e. all observations in the "other" group are from the same year, but do not have the same industry code).

Using these groups, I regressed total accruals on some variables and saved the residuals as "discretionary accruals" (dac_obsv)

Next, I need to match each firm-year observation with another firm-year observation within that group and the match should happen with the observation that has the closest return on assets (ROA). Afterwards, I will use the dac_obsvof the matched observation to calculate the final step (|dac_obsv - dac_matched|).

Hence, my question: How can I match my observations in such a way that they are matched with the observation in the same group that has the closest ROA?

Below, you can find part of my code up till now:
Code:
// Group industries with at least 8 firms and create an "other" group for otherwise
bysort fyear sic2: egen freq_sic2 = count(sic2)
egen group_sic2 = group (fyear sic2)
forvalues i = 2013/2015 {
    replace group_sic2 = `i' if (freq_sic < 8 & fyear == `i')
}

/// Create new group ids with consecutive numbers, so the for-loop will work.
egen group2_sic2 = group (group_sic2)
drop group_sic2

// Regress
gen dac_obsv = 0
forvalues i = 1/31 {
    reg ACC lag_at1 change_sale ppegt if group2_sic2 == `i'
    predict temp, residuals
    replace dac_obsv = temp if temp ~= .
    drop temp
}

// Calculate ROA
gen ROA = ni / at

dataex gvkey2 fyear sic2 group2_sic2 dac_obsv ROA


And my data:
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input long gvkey2 double fyear float(sic2 group2_sic2 dac_obsv ROA)
  7881 2013 10 31    -.1113315    -.09941851
 14590 2013 10 31  .0006080783     .04187607
 29173 2013 13  1     -.095701    .013625148
 14934 2013 13  1   -.07037049 -.00046645055
  1678 2013 13  1   -.06539218    .036212016
120093 2013 13  1 -.0013243398     .05796571
  7620 2013 13  1   -.11223312    .064163685
 11923 2013 13  1   -.08850802    .014359728
  9465 2013 13  1   .010281556     .10032787
  5439 2013 13  1   -.02721703      .0727167
 20548 2013 13  1   -.10563926     .05616714
  7017 2013 13  1   -.04868451     .04921392
 14359 2013 13  1   -.15535808    -.06820373
  8068 2013 13  1   -.04339208     .08500497
  6788 2013 13  1   -.03905627    .015854314
 16478 2013 13  1    -.1132986    .071861446
  8549 2013 13  1  -.008366337     .07755575
  7912 2013 13  1   -.05129356     .04979127
  1380 2013 13  1    .02779204     .11816438
 11228 2013 14 31   .007283384   .0029521224
  8823 2013 15 31     .2458316      .2999855
  4818 2013 16 31   .032945774      .0802166
 66446 2013 17 31   .036919776     .06937753
  3144 2013 20  2    .03234896     .09531952
  3505 2013 20  2    .01196952    .036411833
  5597 2013 20  2    -.0247544     .15314454
180833 2013 20  2  -.006113946      .1869549
 24316 2013 20  2    .00552455      .2384082
142953 2013 20  2  -.002784396     .05395758
  6375 2013 20  2    .05089426     .11677653
  8479 2013 20  2   .016736088     .08699244
179621 2013 21 31   .011967474     .22469084
120877 2013 21 31    .07718544     .11154395
  8543 2013 21 31    .05686508     .13009553
 25119 2013 22 31    .02828753     .04106178
 11456 2013 24 31   .020500677     .03883294
  6649 2013 25 31  -.034171026      .0634793
  6104 2013 26 31    .00071028     .04424638
  7435 2013 26 31   .018824205     .13886736
  6435 2013 26 31   .004452804     .11321951
  7257 2013 28  3  -.014082456     .04168678
 24344 2013 28  3    -.1344164    -.19190174
  4213 2013 28  3    .02112188     .04928577
  8530 2013 28  3   .017168332     .12784934
 62263 2013 28  3    -.0380769      .0762261
  2086 2013 28  3   .006112917      .0777765
163946 2013 28  3    .04844807     .13715923
  4510 2013 28  3    .06639741     .05613921
 23812 2013 28  3  -.022438044     .14380215
  6078 2013 28  3    .02262517      .1061142
 24468 2013 28  3   .008688239     .15698293
  9667 2013 28  3   -.00557985     .11790994
  6730 2013 28  3   .023087414     .13290703
  8247 2013 28  3  .0029861694     .20368153
  4060 2013 28  3   .012006408    .068876706
 13721 2013 28  3    .01897236      .0768527
  3170 2013 28  3   -.01906245     .16150187
 16101 2013 28  3  -.021238964     .14137955
  1602 2013 28  3    .03494855     .07683932
294524 2013 28  3   .016154755     .14129241
 24856 2013 28  3      .050183     .13667767
  2403 2013 28  3    .02687978    .066412725
 29392 2013 28  3     .0391585     .09835374
 13599 2013 28  3  -.008314132      .1083778
 15247 2013 29 31  -.006080544     .05755396
  2991 2013 29 31   .015581252     .08442461
186989 2013 29 31   .010419308      .0744055
170841 2013 29 31   .005373346     .07482228
 10466 2013 29 31   .016248802     .03077153
  4503 2013 29 31     .0408916     .09394247
  5234 2013 30 31   .033350963    .035887487
  9555 2013 30 31   -.01037242     .01359725
  8215 2013 32 31  -.008930976    .021855326
  8030 2013 33 31   .011647358    .032099973
  1988 2013 34 31  -.008292693     .05202179
  7085 2013 34 31  -.005523456     .03923266
316056 2013 34 31   -.06778742    .015657356
  5878 2013 35  4   .005155102     .08409296
  4108 2013 35  4    .04319837      .0963978
  4058 2013 35  4   .031799246     .09255518
  8463 2013 35  4   .018515082     .04571117
  3650 2013 35  4   .008707452     .10069256
  2817 2013 35  4  -.021517944     .04463108
121718 2013 35  4   .009722793     .04259152
189491 2013 35  4    .02357459     .04656863
 60894 2013 35  4    .04000037     .04907183
  8606 2013 35  4 -.0011881363     .02108979
 12053 2013 35  4  -.035254132     .06301119
 63892 2013 35  4    .02177991     .06684477
  3532 2013 36  5   .023636306     .06886017
 14282 2013 36  5   .022842906      .1030592
175404 2013 36  5  -.027236136     .05128756
  4199 2013 36  5    .04134171     .05243583
 10499 2013 36  5   -.01335562       .114162
  7585 2013 36  5   .062775776      .0927348
 11465 2013 36  5    .02295861     .05320381
  6008 2013 36  5   -.06404007      .1041599
 10983 2013 37  6    .04096874     .06314988
118122 2013 37  6  .0006286372     .10971305
  5073 2013 37  6    .01134167     .03213822
end

For your information, I am using Stata 16.1.
I hope this gives you enough information about my question. Please let me know if you can help me out!
And thank you in advance.


Kind regards,
Lianne