#/usr/local/bin/perl
$d1=time;
$hd=join "\t",("personID","SEER","race","sex","aDiag","bYear","sequen","yDiag","morph","ageRC","ICD9","numPrim","surv","COD");
@files=('breast','digothr','malegen','femgen','other','respir','colrect','lymyleuk','urinary');
#@files=('test');
chdir("c\:\\data\\seer\\incidence\\yr1973_2008.seer9");
foreach $datafile (@files)
 {
  open (INTEXT, ($datafile . ".txt"));
  open( OUTTEXT, (">c\:\\data\\seer\\stx\\" . $datafile . ".stx" ));
  printf OUTTEXT "$hd\n";
  print "$datafile\n";
while ( <INTEXT> ) {  # this takes about 60 seconds, i.e. it is 10-fold faster than the same code in R 
	$personID=substr($_,0,8);
	$region=substr($_,16,2);
	$race=substr($_,19,2);
	$sex=substr($_,23,1);
	$aDiag=substr($_,24,3);
	$bYear=substr($_,27,4);
	$sequen=substr($_,34,2);
	$yDiag=substr($_,38,4);
	$morph=substr($_,47,4);
	$ageRC=substr($_,182,2);
	$ICD9=substr($_,194,4);
	$numPrim=substr($_,232,2);
	$srvy=substr($_,240,2);
	$srvm=substr($_,242,2);
	$COD=substr($_,244,5);
	$surv=12*$srvy+$srvm; # months of survival
	$y=join "\t",($personID,$region,$race,$sex,$aDiag,$bYear,$sequen,$yDiag,$morph,$ageRC,$ICD9,$numPrim,$surv,$COD);
	printf OUTTEXT "$y\n";
  }
close INTEXT;
close OUTTEXT;
}
print "done!\n";
$d2=time;
print $d2-$d1;
exit
