--- rleda/rleda.c 2005/02/08 17:10:08 1.3 +++ rleda/rleda.c 2005/02/14 12:15:06 1.4 @@ -1,9 +1,13 @@ -/* $Id: rleda.c,v 1.3 2005/02/08 17:10:08 brouard Exp $ +/* $Id: rleda.c,v 1.4 2005/02/14 12:15:06 brouard Exp $ $State: Exp $ $Log: rleda.c,v $ - Revision 1.3 2005/02/08 17:10:08 brouard - Lit le fichier index deux fois pour n'avoir qu'un passage en SAS. - AJoute le proc contents des fichiers créés en fin de run; + Revision 1.4 2005/02/14 12:15:06 brouard + Garde une variable intitulée numero qui relit les niveaux 2 et 3, tant + en SAS qu'en STATA. + + Revision 1.3 2005/02/08 17:10:08 brouard + Lit le fichier index deux fois pour n'avoir qu'un passage en SAS. + AJoute le proc contents des fichiers créés en fin de run; Revision 1.2 2003/12/12 19:08:17 brouard Les variables ne sont plus toutes écrites en ligne mais avec un retour @@ -40,16 +44,18 @@ #define ODIRSEPARATOR '/' #endif -char version[]="rleda-0.9.2 Programme de lecture de fichiers d'index au format LEDA convertis de l'EBCDIC en ASCII "; -char fullversion[]="$Revision: 1.3 $ $Date: 2005/02/08 17:10:08 $"; +char version[]="rleda-0.9.5 Programme de lecture de fichiers d'index au format LEDA convertis de l'EBCDIC en ASCII "; +char fullversion[]="$Revision: 1.4 $ $Date: 2005/02/14 12:15:06 $"; char line[MAXLINE]; char title[MAXLINE],namevar[MAXLINE], namevara[MAXLINE]; char fileres[FILENAMELENGTH], filereso[FILENAMELENGTH]; char filesas[FILENAMELENGTH]; /* SAS file */ +char filestata[FILENAMELENGTH]; /* STATA file */ +char filestatal[10][FILENAMELENGTH]; /* STATA dictionary files */ char filelog[FILENAMELENGTH]; /* Log file */ -FILE *ficlog, *ficres, *ficdat, *ficreso, *ficsas; +FILE *ficlog, *ficres, *ficdat, *ficreso, *ficsas, *ficstata, *ficstatal[10]; static unsigned char const ascii_to_ebcdic[] = @@ -235,7 +241,7 @@ void trzb(char *z, char *in) main(int argc, char *argv[]) { - int numlinepar=0, ilect=0, firstpass=0; + int numlinepar=0, ilect=0, firstpass=0, idic[10]={0,0,0,0,0,0,0,0,0,0}; long nobs=0, ncar=0,ncara=0, ncartes=0, nn=0, nvaris=0; long c; long d; @@ -304,8 +310,12 @@ main(int argc, char *argv[]) strcat(filesas,indexfilefiname); strcat(filesas,".sas"); + strcpy(filestata,"s"); + strcat(filestata,indexfilefiname); + strcat(filestata,".do"); printf("Le nom du fichier de sortie est %s\n",filereso); printf("Le nom du fichier SAS de sortie est %s\n",filesas); + printf("Le nom du fichier STATA de sortie est %s\n",filestata); for(ilect=1;ilect<=2;ilect++){ pos=9;nvaris=0 ; @@ -319,8 +329,8 @@ main(int argc, char *argv[]) } if(ilect==2){ if((ficsas=fopen(filesas,"w"))==NULL) { /* opened on subdirectory */ - printf("Problem with Output resultfile: %s\n", filesas); - fprintf(ficlog,"Problem with Output resultfile: %s\n", filesas); + printf("Problem with Output SAS file: %s\n", filesas); + fprintf(ficlog,"Problem with Output SAS file: %s\n", filesas); fflush(ficlog); goto end; } @@ -329,14 +339,31 @@ main(int argc, char *argv[]) fprintf(ficsas,"filename a \"%s.dat\";\n",indexfilefiname ); fprintf(ficsas,"* Ci dessous a modifier eventuellement *;\n"); fprintf(ficsas,"data %s.%s0 (keep=ID)\n",indexfilefiname,indexfilefiname); - for(level=1; level <=levelmax; level++){ - fprintf(ficsas," %s.%s%d (keep=%s--%s)\n",indexfilefiname,indexfilefiname,level,var1[level],varn[level]); + fprintf(ficsas," %s.%s%d (keep=%s--%s)\n",indexfilefiname,indexfilefiname,1,var1[1],varn[1]); + idic[1]=0; + for(level=2; level <=levelmax; level++){ + fprintf(ficsas," %s.%s%d (keep=numero %s--%s)\n",indexfilefiname,indexfilefiname,level,var1[level],varn[level]); + idic[level]=0; } fprintf(ficsas," ;\n"); fprintf(ficsas," infile a lrecl=500 ;\n"); - fprintf(ficsas," input id 1. @; \n"); + fprintf(ficsas," input id 1. @; retain numero 0; \n"); fprintf(ficsas,""); fprintf(ficsas,""); + if((ficstata=fopen(filestata,"w"))==NULL) { /* opened on subdirectory */ + printf("Problem with Output Stata file: %s\n", filestata); + fprintf(ficlog,"Problem with Output Stata: %s\n", filestata); + fflush(ficlog); + goto end; + } + fprintf(ficstata,"*Sortie de rleda version %s *;\n* %s *;\n",version,fullversion); + fprintf(ficstata,"*repertoire %s \"%s\";\n",indexfilefiname,path); + fprintf(ficstata,"* Ci dessous a modifier eventuellement *;\n"); + fprintf(ficstata,"* data %s.%s0 (keep=ID)\n",indexfilefiname,indexfilefiname); + for(level=1; level <=levelmax; level++){ + fprintf(ficstata,"* %s.%s%d (keep=%s--%s)\n",indexfilefiname,indexfilefiname,level,var1[level],varn[level]); + } + fprintf(ficstata,"* ;\n"); } while((c=getc(ficdat)) != EOF){ ungetc(c,ficdat); @@ -382,6 +409,7 @@ main(int argc, char *argv[]) if(level ==0){ if(ilect==2){ fprintf(ficsas," if id=%d then do;\n",level); + /* fprintf(ficstata,"* if id=%d then do;\n",level); */ /* fprintf(ficsas," input @9 "); */ } } @@ -433,15 +461,39 @@ main(int argc, char *argv[]) pos=9; firstpass=0; if(ilect==2){ - fprintf(ficsas," ;\n output %s.%s%d;\n",indexfilefiname,indexfilefiname,olevel); + if(olevel==1){ + fprintf(ficsas," ;\n output %s.%s%d;\n",indexfilefiname,indexfilefiname,olevel); + }else if (olevel==2){ + fprintf(ficsas," ;\n numero +1;\n output %s.%s%d;\n",indexfilefiname,indexfilefiname,olevel); + } + fflush(ficsas); + if(idic[olevel]!=0) + fprintf(ficstatal[olevel],"\n}\n"); fprintf(ficsas," end\n"); } - if(level !=0) + if(level !=0){ if(ilect==2){ fprintf(ficsas," ;\n else if id=%d then do;\n",level); + if(idic[level]==0){ + sprintf(filestatal[level],"s%s-dict%d.dct",indexfilefiname,level); + fprintf(ficlog,"\nOutput Stata dictionary: %s\n", filestatal[level]); + if((ficstatal[level]=fopen(filestatal[level],"w"))==NULL) { /* opened on subdirectory */ + printf("Problem with Output Stata dictionary file: %s\n", filestatal[level]); + fprintf(ficlog,"Problem with Output Stata dictionary: %s\n", filestatal[level]); + fflush(ficlog); + goto end; + } + idic[level]=1; +/* fprintf(ficstatal[level],"* Sortie de rleda version %s, %s\n",version,fullversion ); */ +/* fprintf(ficstatal[level],"* Nom de fichier %s et chemin \"%s\";\n* Stata dictionary\n",indexfilefiname,path); */ + fprintf(ficstatal[level],"dictionary using \"%s.dat\"{\n",indexfilefiname ); + } } + } if(ilect==2){ fprintf(ficsas," input @9 "); + if(idic[level]!=0) + fprintf(ficstatal[level]," str1 id _column(9) "); printf(" input @9 "); } if(ilect==1){ @@ -469,11 +521,13 @@ main(int argc, char *argv[]) } if(ilect==2){ fprintf(ficsas, " %s $%d.", namevar, ncara); + fprintf(ficstatal[level], " str%d %s %%%ds", ncara, namevar, ncara); } pos+=ncara;nvaris++ ; if (nvaris>=5) { if(ilect==2){ fprintf(ficsas,"\n ") ; + fprintf(ficstatal[level],"\n ") ; } nvaris=0 ; } @@ -522,13 +576,28 @@ main(int argc, char *argv[]) } /* End loop ilect */ fprintf(ficsas," ;\n output %s.%s%d;\n end;\n else input;\nrun;\n",indexfilefiname,indexfilefiname,level); + fprintf(ficstatal[level],"\n}\n"); for(level=0; level <=levelmax; level++){ fprintf(ficsas,"proc contents data=%s.%s%d;run;\n",indexfilefiname,indexfilefiname,level); + /* fprintf(ficstata,"*proc contents data=%s.%s%d;run;\n",indexfilefiname,indexfilefiname,level); */ + if(level==2){ + fprintf(ficstata,"clear\n quiet infile using s%s-dict%d if id==\"%d\"\n gen long numero=_n\n sort numero\nsave %s%d,replace\n",indexfilefiname,level,level,indexfilefiname,level); + } + else if(level==3){ + fprintf(ficstata,"clear\n quiet infile using s%s-dict%d if id != \"1\"\n gen long numero=1 if id==\"2\"\n replace numero=sum(numero)\n sort numero\n",indexfilefiname,level); + fprintf(ficstata,"* merge numero using %s2\n* drop numero\n drop if id != \"3\"\nsave %s%d,replace\n",indexfilefiname,indexfilefiname,level); + } } fclose(ficsas); + fclose(ficstata); printf("Le nom du fichier de debug est %s\n",filelog); printf("Le nom du fichier du format de sortie est %s\n",filereso); printf("Le nom du fichier SAS de sortie est %s\n",filesas); + printf("Le nom du fichier STATA de sortie est %s\n",filestata); + for(level=1;level<=levelmax;level++){ + fclose(ficstatal[level]); + printf("Le nom du fichier STATA dictionnaire niveau %d de sortie est %s\n",level,filestatal[level]); + } fclose(ficreso); fclose(ficlog); exit(0);