Dear all, The following question is taken from here (http://bbs.pinggu.org/forum.php?mod=...=1#pid50149617). The data is
The purpose is to obtain the cumulative frequencies across the columns for each row (comp_ctry). My answer is:
and the desired result is
My question is that: Is there a simple command that can calculate the cumulative frequencies for each row (comp_ctry)?
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str13 comp_ctry float(rela_00 rela_01 rela_02 rela_03 rela_04 rela_05) "4401210001102" . 1 1 . . . "4401210001103" . 1 1 1 . . "4401210001106" . 1 1 . . . "4401210001107" 1 . 1 . . . "4401210001108" 1 . 1 1 . . "4401210001110" 1 1 1 1 1 . "4401210001111" 1 1 1 1 . . "4401210001112" 1 1 1 1 . . end
Code:
ren rela_(##) rela_20(##) ren rela_* yr* reshape long yr, i(comp_ctry) j(year) bys comp_ctry (year): gen n = sum(yr) replace n = . if yr == . drop yr reshape wide n, i(comp_ctry) j(year) ren n20(##) rela_(##)
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str13 comp_ctry float(rela_00 rela_01 rela_02 rela_03 rela_04 rela_05) "4401210001102" . 1 2 . . . "4401210001103" . 1 2 3 . . "4401210001106" . 1 2 . . . "4401210001107" 1 . 2 . . . "4401210001108" 1 . 2 3 . . "4401210001110" 1 2 3 4 5 . "4401210001111" 1 2 3 4 . . "4401210001112" 1 2 3 4 . . end
Comment