Hello everyone,

In a paper I am writing for my studies, I want to recreate a proxy measuring earnings management, developed by Leuz et al. (2003).
I am relatively new to STATA, and I have a question regarding the creation of four variables.

I analyse companies from 28 countries from 2016-2019.

Herefore, I need to create the following variables:

EM 1, defined as the county's median ratio of the firm-level standard deviations of OPPL and OCF.

EM 2, defined as the county-year's spearman correlation between the Delta_ACCR and Delta_OCF.

EM 3, defined as the country-year's median ratio of the absolute value of DACR and the absolute value of CFO

EM 4, defined as the number of SL divided by the number of SP for each country-year.

I have researched and tried some options now, but I am having a hard time with realizing the country-year levels. E.g., for the first variable I tried the following, having stopped at some point (please excuse me if the approach seems unlogical, but I am really new to STATA coding!)
sum OPPL,d
gen standOPPL = r(sd)
sum standOPPL

sum CFO,d
gen standCFO = r(sd)
sum standCFO

gen REM1 = standOPPL/standCFO
sum REM1,d
gen EM1 = r(median)

I am not sure if it's common to ask for such general coding advice, but I would really appreciate it if someone could help me out here as I don't have someone I can ask in my personal environment.

Best regards from Germany, and thank you in advance

Elena