// version 1.0.0 14Feb2018 #include "stplugin.h" #include #include #include STDLL stata_call(int argc, char *argv[]) { ST_int i, j, nObs, nVars ; ST_int first, last ; ST_double value ; ST_retcode rc ; int nchar ; char line[82], strval[27], msg[81] ; rc = (ST_retcode) 0 ; // Put the first observation in sample into first first = SF_in1(); // Put the last observation in sample into last last = SF_in2(); // Put number of variables in varlist passed in to plugin into nVars nVars = SF_nvars() ; // Initiate number of observations counter to 0 nObs = 0 ; // Loop over observations for(i=first; i<=last; i++) { line[0] = '\0' ; // Only display observations for which if restriction is true if (!SF_ifobs(i)) { continue ; } // Increment number of observations counter ++nObs ; // Loop over variables for(j=1; j<=nVars; j++) { // Put value of observation i on variable j into value rc = SF_vdata(j, i, &value); // Return with error if problem getting value if(rc>0) { sprintf(msg, "Problem accessing Stata data\n") ; SF_error(msg) ; return(rc) ; } // Return with error if missing value if (SF_is_missing(value)) { sprintf(msg, "missing values encountered\n") ; SF_error(msg) ; return( (ST_retcode) 416 ) ; } nchar = snprintf(strval,25,"%f ",value) ; // Return with error if number is too large or cannot be // formated into string as float if (nchar<=0 || nchar>25) { sprintf(msg, "number is too large or badly formated\n") ; SF_error(msg) ; return( (ST_retcode) 498 ) ; } // If new version of line is not too big, concatenate string value onto line if ( (strlen(strval) + strlen(line))<=80) { strcat(line,strval) ; } else { sprintf(msg, "More than 80 bytes in line\n") ; SF_error(msg) ; return( (ST_retcode) 498 ) ; } } // We know that line has 80 bytes or less, so next line is safe strcat(line,"\n") ; // Display line in Stata SF_display(line) ; } sprintf(line, "First observation was %d\n", first) ; SF_display(line) ; sprintf(line, "Last observation was %d\n", last) ; SF_display(line) ; sprintf(line, "Number of observations listed was %d\n", nObs) ; SF_display(line) ; return(rc) ; }