That code looks fine to me, other than your use of tag_type in the list command, instead of the variable tag_nos that you created.
-
Login or Register
- Log in with
gen byte yr5 = 0 if grant_year == 2001 & !missing(grant_year) replace yr5 = 1 if inrange(grant_year, 2002, 2006) replace yr5 = 2 if inrange(grant_year, 2007, 2011) replace yr5 = 3 if inrange(grant_year, 2012, 2017) replace yr5 = 4 if inrange(grant_year, 2018, 2023) label define yr5 1 "[1] 2002 to 2006" 2 "[2] 2007 to 2011" 3 "[3] 2012 to 2017" 4 "[4] 2018 to 2023", modify label values yr5 yr5 tab yr5 yr5 | Freq. Percent Cum. -----------------+----------------------------------- 0 | 20 0.02 0.02 [1] 2002 to 2006 | 28,056 26.27 26.29 [2] 2007 to 2011 | 26,101 24.44 50.73 [3] 2012 to 2017 | 27,787 26.02 76.75 [4] 2018 to 2023 | 24,831 23.25 100.00 -----------------+----------------------------------- Total | 106,795 100.00
egen grant_name_yr = total(grant2), by(grant_year investigator_name) egen tag_name_yr = tag(investigator_name) // I could easily then swap between 'grant_year' and 'yr5' gsort -tag_name_yr -grant_name_yr format %16.0gc grant2 grant_name_yr list grant_year grant_name_yr investigator_name in 1/20 if tag_name_yr, abbrev(20) noobs
grant_year grant_name_yr investigator_name | |---------------------------------------------------------| | 2003 300,987,008 Adr Tur | | 2020 72,452,928 Col Jac | | 2020 71,822,240 Bra She | | 2023 70,350,000 Nat Cur | | 2023 70,298,584 Sco Fos | |---------------------------------------------------------| | 2023 70,298,584 Iri Kab | | 2017 69,831,296 Mat Dav | | 2017 69,782,288 Jar Co | | 2020 68,816,312 Sim Smi | | 2017 68,211,824 Kir Mck | |---------------------------------------------------------| | 2010 54,195,532 Tor Leh | | 2010 53,833,512 Rob Guy | | 2010 53,586,600 Jon Cro | | 2010 53,586,600 Joh Mor | | 2010 53,586,600 Jul Qui | |---------------------------------------------------------| | 2003 50,000,000 Dav Col | | 2011 44,070,168 Dav Pan | | 2020 42,067,244 Elh Dor | | 2023 41,636,032 Shi Qia | | 2023 41,613,760 Hei Ebe
gsort grant_year -nos_grant bysort grant_year: gen top_one = (_n == 1)
. list grant_year investigator_name nos_grant if top_one, abbrev(20) sep(0) noobs +--------------------------------------------+ | grant_year investigator_name nos_grant | |--------------------------------------------| | 2019 Joh Car 3 | | 2020 Joh Car 3 | | 2021 Bry Bor 2 | | 2022 Man Che 2 | | 2023 Ale Com 2 | +--------------------------------------------+
egen top_one = rank(nos_grant), field by(grant_year)
. list grant_year investigator_name nos_grant if top_one == 1, abbrev(20) sep(0) noobs +--------------------------------------------+ | grant_year investigator_name nos_grant | |--------------------------------------------| | 2019 Joh Car 3 | | 2020 Joh Car 3 | | 2020 Joh Car 3 | | 2021 Bry Bor 2 | | 2021 Ale Com 2 | | 2022 Man Che 2 | | 2022 Ana Roz 2 | | 2023 Ale Com 2 | | 2023 Ana Roz 2 | | 2023 Bry Bor 2 | +--------------------------------------------+
gsort grant_year -grant_investigator bysort grant_year: gen top_one = (_n == 1) list grant_year grant_total investigator_name if top_one, abbrev(20) sep(0) noobs
split other_investigator, gen(investigator) p(;) drop other_investigator rename lead_investigator investigator0 gen `c(obs_t)' x = _n // added - create temp var to uniquely id each obs. reshape long investigator, i(x id) j(num) // i(x id grant2) 'x' added to account for new code replace investigator = trim(itrim(investigator)) drop if missing(investigator) drop x
Comment