My dataset contains an identifier variable id, a total score tfcscore, and several subscores this total score is made up from. I would like to construct some code which returns for each distinct id the total tfcscore, and which subscores (tfclfu_total****) make up this total score. I planned to do this by listing the ids using a foreach loop, then nesting a loop in there which checks which subscores !=0, then prints those. However, if I try to nest the loop after the initial list id if tfcscore>3 & pedigree==1, Stata prints a different list instead. I am quite new to programming in Stata.
How do I correctly tell Stata to return a list of ids with the 'aberrant' (so value>0) subscores listed behind it? Ideally, the result from the dataex below would look something like:
0006 | tfcscore: 2
tfclfu_totalrepo: 2
This is where I currently am stuck with my code (some things below redacted due to confidentiality):
Code:
//gen pathgen = max(some other scores) //gen tfcfamscore = max(some other scores) //gen tfcscore = (tfclfu_totalrepo/depo/arrh combined) //gen tfcscore_nogen = (some other code) //egen groupid = group(id) summarize groupid, meanonly foreach i of num 1/`r(max)' { list id if tfcscore>1 & pedigree==1 //Now I want to check for each id this returns: the value of tfclfu_totalrepo, and I want to print/display it if this value !=0. The same for tfclfu_totaldepo and some other vars. }
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str24 id float tfcscore byte(pedigree tfclfu_totaldepo tfclfu_totalrepo tfclfu_totalarrh) "0001" 0 2 0 0 0 "0002" 0 2 0 0 0 "0002" . . . . . "0003" 1 2 0 1 0 "0003" . . . . . "0003" . . . . . "0006" 2 1 0 2 0 "0006" . . . . . "0006" . . . . . "0006" . . . . . "0006" . . . . . "0010" 0 2 0 0 0 "0010" . . . . . "0013" 0 2 0 0 0 "0013" . . . . . "0013" . . . . . "0014" 2 2 0 2 0 "0014" . . . . . "0015" 1 1 0 0 1 "0015" . . . . . "0015" . . . . . "0016" 1 1 0 1 0 "0016" . . . . . "0016" . . . . . "0019" 4 2 2 1 1 "0019" . . . . . "0019" . . . . . "0020" 0 1 0 0 0 "0020" . . . . . "0020" . . . . . "0020" . . . . . "0020" . . . . . "0020" . . . . . "0020" . . . . . "0020" . . . . . "0020" . . . . . "0021" 0 2 0 0 0 "0021" . . . . . "0021" . . . . . "0022" 0 2 0 0 0 "0022" . . . . . "0022" . . . . . "0023" 3 2 0 0 1 "0023" . . . . . "0023" . . . . . "0023" . . . . . "0023" . . . . . "0023" . . . . . "0023" . . . . . "0024" 2 2 0 2 0 "0024" . . . . . "0024" . . . . . "0025" 2 1 0 2 0 "0025" . . . . . "0025" . . . . . "0026" 2 2 0 2 0 "0026" . . . . . "0026" . . . . . "0028" 0 1 0 0 0 "0028" . . . . . "0028" . . . . . "0029" 3 2 0 1 0 "0029" . . . . . "0030" 2 2 0 0 0 "0033" 2 2 0 1 1 "0033" . . . . . "0033" . . . . . "0033" . . . . . "0034" 3 1 0 0 1 "0034" . . . . . "0034" . . . . . "0034" . . . . . "0035" 0 1 0 0 0 "0035" . . . . . "0035" . . . . . "0035" . . . . . "0036" 3 1 1 1 1 "0036" . . . . . "0036" . . . . . "0036" . . . . . "0037" 1 2 0 1 0 "0037" . . . . . "0037" . . . . . "0038" 1 2 0 0 1 "0038" . . . . . "0039" 0 2 0 0 0 "0039" . . . . . "0040" 1 2 0 0 1 "0040" . . . . . "0042" 6 2 1 2 1 "0042" . . . . . "0042" . . . . . "0042" . . . . . "0042" . . . . . "0043" 2 2 0 2 0 "0043" . . . . . "0043" . . . . . "0043" . . . . . "0043" . . . . . "0044" 0 2 0 0 0 end
0 Response to Nesting foreach loops to check and print variables
Post a Comment