Package com.stata.sfi
Class Data
java.lang.Object
com.stata.sfi.Data
This class provides access to the current Stata dataset. All variable and
 observation numbering begins at 1 unless otherwise stated.
 
Example:
This example shows how to handle a Stata varlist along with if and in to restrict observations. The example calculates summary statistics and displays a table similar to Stata's summarize command.
public class Examples 
{
	// call directly from integrated environment using java or java:
	public static void summarize() {
		int rc = summarize(null);
		SFIToolkit.setRC(rc);
	}
	
	// call directly from a plugin
	public static int summarize(String args[]) {
		int parsedVariables = Data.getParsedVarCount();
		long obsStart = Data.getObsParsedIn1();
		long obsEnd = Data.getObsParsedIn2();
		if (parsedVariables <= 0) {
			SFIToolkit.errorln("varlist required");
			return 100;
		}
		// display the header
		SFIToolkit.displayln("\n" +   "    " +
				"Variable {c |}        Obs        Mean    Std. Dev.       Min        Max");
		for (int v = 1; v <= parsedVariables; v++) {
			double sum = 0;
			double max = Double.NEGATIVE_INFINITY;
			double min = Double.POSITIVE_INFINITY;
			double mean = 0;
			double stddev = 0;
			long count = 0;
			// get the real variable index for the ith parsed variable
			int varIndex = Data.mapParsedVarIndex(v);
			if (!Data.isVarTypeStr(varIndex)) {
				// calculate mean
				for (long obs = obsStart; obs <= obsEnd; obs++) {
					if (! Data.isParsedIfTrue(obs)) {
						continue;
					}
					double value = Data.getNum(varIndex, obs);
					if (Missing.isMissing(value)) {
						continue ;
					}
					max = Math.max(max, value);
					min = Math.min(min, value);
					sum += value;
					count++;
				}
				mean = sum / count;
				// calculate std. dev.
				double d2sum = 0;
				for (long obs = obsStart; obs <= obsEnd; obs++) {
					if (! Data.isParsedIfTrue(obs)) {
						continue;
					}
					double value = Data.getNum(varIndex, obs);
					if (Missing.isMissing(value)) {
						continue ;
					}
					d2sum += Math.pow(value-mean,2);
				}
				stddev = Math.sqrt(d2sum/(count-1));
			}
			// write out the results
			if (v % 5 == 1) {
				SFIToolkit.displayln("{hline 13}{c +}{hline 57}");
			}
			String out = String.format("%12s {c |}%11s", 
					Data.getVarName(varIndex),
					SFIToolkit.formatValue(count, "%11.0fc"));
			if (count>0) {
				out += String.format("   %9s   %9s  %9s  %9s",
						SFIToolkit.formatValue(mean,  "%9.0g"),
						SFIToolkit.formatValue(stddev,"%9.0g"),
						SFIToolkit.formatValue(min,   "%9.0g"),
						SFIToolkit.formatValue(max,   "%9.0g"));
			}
			SFIToolkit.displayln(out);
			SFIToolkit.pollnow();
			// outer loop; poll each time to update display
			// avoid polling too often; use pollstd() when possible
		}
		return 0;
	}
}
   From Stata... 
. sysuse auto, clear
(1978 Automobile Data)
// call summarize from integrated Java environment; assumes that Examples
// class is defined in Stata do-file that has already been run
. java rep if mpg > 22 in 12/50: Examples.summarize()
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+----------------------------------------------------------
       rep78 |          8        3.25     1.38873          1          5
// call summarize as a Java plugin; assumes that Examples class
// is compiled and archived to examples.jar
. javacall Examples summarize rep if mpg > 22 in 12/50, jar(examples.jar)
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+----------------------------------------------------------
       rep78 |          8        3.25     1.38873          1          5
// compare with built-in summarize command
. summarize rep if mpg > 22 in 12/50
    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       rep78 |          8        3.25     1.38873          1          5
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final intstatic final int
- 
Method SummaryModifier and TypeMethodDescriptionstatic intaddVarByte(String name) Add a variable of type byte to the current Stata dataset.static intaddVarDouble(String name) Add a variable of type double to the current Stata dataset.static intaddVarFloat(String name) Add a variable of type float to the current Stata dataset.static intAdd a variable of type int to the current Stata dataset.static intaddVarLong(String name) Add a variable of type long to the current Stata dataset.static intAdd a variable of type str to the current Stata dataset.static intaddVarStrL(String name) Add a variable of type strL to the current Stata dataset.static intallocateStrL(StrLConnector sc, long size) Allocate a strL so that a buffer can be stored usingwriteBytes; the contents of the strL will not be initialized.static intallocateStrL(StrLConnector sc, long size, boolean binary) Allocate a strL so that a buffer can be stored usingwriteBytes; the contents of the strL will not be initialized.static intdropVar(int var) Drop the variable at the specified variable index.static intgetBestType(double value) Get the best numeric data type for the specified value.static StringgetFormattedValue(int var, long obs, boolean bValueLabel) Read a value from the current Stata dataset, applying its display format.static intGet the maximum length of a Stata string variable of type str.static intGet the maximum number of variables Stata currently allows.static doublegetNum(int var, long obs) Read a numeric value from the current Stata dataset.static longGet the first in a range of observations if Java was invoked with the in qualifier.static longGet the last in a range of observations if Java was invoked with the in qualifier.static longGet the number of observations in the current Stata dataset.static intGet the number of variables specified when Java was invoked.static DoubleGet the double representation of aStringusing Stata's real() function.static StringgetStr(int var, long obs) Read a string value from the current Stata dataset; this method can be used to read str or strL data types.static StringgetStrf(int var, long obs) Read a string value from the current Stata dataset; this method can be used to read str data types.static intgetStrVarWidth(int var) Get the width of a variable of type str.static intgetType(int var) Get the data type for the specified variable.static intGet the number of variables in the current Stata dataset.static StringgetVarFormat(int var) Get the format for a Stata variable.static intgetVarIndex(String varname) Look up the variable index for the specified name in the current Stata dataset.static StringgetVarLabel(int var) Get the label for a Stata variable.static StringgetVarName(int var) Get the variable name at a given variable index.static booleanisAlias(int var) Determine if a variable is an alias for a variable in another frame.static booleanisParsedIfTrue(long obs) Determine if an observation for the if expression qualifier used when Java was invoked is true or false.static booleanDetermine if a varlist was specified when Java was invoked.static booleanisVarTypeStr(int var) Test if a variable is of type str.static booleanisVarTypeString(int var) Test if a variable's type is string.static booleanisVarTypeStrL(int var) Test if a variable is of type strL.static StringmakeVarName(String s, boolean retainCase) Attempt to form a valid variable name from a string.static intmapParsedVarIndex(int var) Map the variable index from the parsed varlist.static intreadBytes(StrLConnector sc, byte[] b) Read a sequence of bytes from a strL.static intreadBytes(StrLConnector sc, byte[] b, int off, int len) Read a sequence of bytes from a strL.static intRename a Stata variable.static intsetObsTotal(long obs) Set the number of observations in the current Stata dataset.static intsetVarFormat(int var, String format) Set the format for a Stata variable.static intsetVarLabel(int var, String label) Set the label for a Stata variable.static intstoreBytes(StrLConnector sc, byte[] bytes, boolean binary) Store a byte buffer to a strL.static intstoreNum(int var, long obs, double value) Store a numeric value in the current Stata dataset.static intstoreNumFast(int var, long obs, double value) Store a numeric value in the current Stata dataset.static intStore a string value in the current Stata dataset; this method can be used to store str or strL data types.static intStore a string value in the current Stata dataset; this method can be used to store str data types.static intstoreStrfFast(int var, long obs, String value) Store a string value in the current Stata dataset; this method can be used to store str data types.static voidInform Stata that its data has been modified.static intwriteBytes(StrLConnector sc, byte[] b) Write a byte buffer to a strL; the strL must be allocated usingallocateStrLbefore calling this method.static intwriteBytes(StrLConnector sc, byte[] b, int off, int len) Writelenbytes from the specified byte buffer starting at offsetoffto a strL; the strL must be allocated usingallocateStrLbefore calling this method.
- 
Field Details- 
TYPE_UNKNOWNpublic static final int TYPE_UNKNOWN- See Also:
 
- 
TYPE_BYTEpublic static final int TYPE_BYTE- See Also:
 
- 
TYPE_INTpublic static final int TYPE_INT- See Also:
 
- 
TYPE_LONGpublic static final int TYPE_LONG- See Also:
 
- 
TYPE_FLOATpublic static final int TYPE_FLOAT- See Also:
 
- 
TYPE_DOUBLEpublic static final int TYPE_DOUBLE- See Also:
 
- 
TYPE_STRpublic static final int TYPE_STR- See Also:
 
- 
TYPE_STRLpublic static final int TYPE_STRL- See Also:
 
 
- 
- 
Method Details- 
addVarByteAdd a variable of type byte to the current Stata dataset.- Parameters:
- name- Name of the variable to be created.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
addVarDoubleAdd a variable of type double to the current Stata dataset.- Parameters:
- name- Name of the variable to be created.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
addVarFloatAdd a variable of type float to the current Stata dataset.- Parameters:
- name- Name of the variable to be created.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
addVarIntAdd a variable of type int to the current Stata dataset.- Parameters:
- name- Name of the variable to be created.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
addVarLongAdd a variable of type long to the current Stata dataset.- Parameters:
- name- Name of the variable to be created.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
addVarStrAdd a variable of type str to the current Stata dataset.- Parameters:
- name- Name of the variable to be created.
- length- Initial size of the variable. If the length is greater than- getMaxStrLength(), then a variable of type strL will be created.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
addVarStrLAdd a variable of type strL to the current Stata dataset.- Parameters:
- name- Name of the variable to be created.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
allocateStrLAllocate a strL so that a buffer can be stored usingwriteBytes; the contents of the strL will not be initialized. By default, the data will be marked as binary.- Parameters:
- sc- The StrLConnector representing a strL.
- size- The size in bytes.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
allocateStrLAllocate a strL so that a buffer can be stored usingwriteBytes; the contents of the strL will not be initialized.- Parameters:
- sc- The StrLConnector representing a strL.
- size- The size in bytes.
- binary- Mark the data as binary. Note that if the data are not marked as binary, Stata expects that the data be UTF-8 encoded. An alternate approach is to call- storeStr, where the encoding is automatically handled.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
dropVarDrop the variable at the specified variable index.- Parameters:
- var- Variable to drop.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
getBestTypeGet the best numeric data type for the specified value.- Parameters:
- value- The value to test.
- Returns:
- The field value representing the data type; may be
         TYPE_BYTE,TYPE_INT,TYPE_LONG,TYPE_FLOAT, orTYPE_DOUBLE.
 
- 
getFormattedValueRead a value from the current Stata dataset, applying its display format.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- bValueLabel- Use the value label when available.
- Returns:
- The formatted value as a String.
 
- 
getMaxStrLengthGet the maximum length of a Stata string variable of type str.- Returns:
- The maximum length.
 
- 
getMaxVarsGet the maximum number of variables Stata currently allows.- Returns:
- The maximum number of variables.
 
- 
getNumRead a numeric value from the current Stata dataset.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- Returns:
- The value.
 
- 
getObsParsedIn1Get the first in a range of observations if Java was invoked with the in qualifier. If in was not specified, then the range will reflect the entire dataset.- Returns:
- The first observation's number.
 
- 
getObsParsedIn2Get the last in a range of observations if Java was invoked with the in qualifier. If in was not specified, then the range will reflect the entire dataset.- Returns:
- The last observation's number.
 
- 
getObsTotalGet the number of observations in the current Stata dataset.- Returns:
- The number of observations.
 
- 
getParsedVarCountGet the number of variables specified when Java was invoked. If a varlist was not specified, then all the variables are implied.- Returns:
- The number of variables.
 
- 
getRealOfStringGet the double representation of aStringusing Stata's real() function.- Parameters:
- s- The string to convert.
- Returns:
- The numeric value. Returns null if an error occurs.
 
- 
getStrRead a string value from the current Stata dataset; this method can be used to read str or strL data types.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- Returns:
- The String. Returns null if an error occurs.
 
- 
getStrfRead a string value from the current Stata dataset; this method can be used to read str data types.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- Returns:
- The String. Returns null if an error occurs.
 
- 
getStrVarWidthGet the width of a variable of type str.- Parameters:
- var- The index of the variable to test.
- Returns:
- The width if the variable is of type str.
 
- 
getTypeGet the data type for the specified variable.- Parameters:
- var- Variable to access.
- Returns:
- The value representing the data type; may be TYPE_BYTE,TYPE_INT,TYPE_LONG,TYPE_FLOAT,TYPE_DOUBLE,TYPE_STR,TYPE_STRL, orTYPE_UNKNOWN.
 
- 
getVarCountGet the number of variables in the current Stata dataset.- Returns:
- The number of variables.
 
- 
getVarFormatGet the format for a Stata variable.- Parameters:
- var- Index of the variable to look up.
- Returns:
- The variable's format.
 
- 
getVarIndexLook up the variable index for the specified name in the current Stata dataset.- Parameters:
- varname- Name of the variable.
- Returns:
- The variable index. If the variable does not exist, 0 is
         returned. 
 
 Note: When Stata version control is less than 15.0 and the variable does not exist, the number of variables plus one will be returned.
 
- 
getVarLabelGet the label for a Stata variable.- Parameters:
- var- Index of the variable to look up.
- Returns:
- The variable's label.
 
- 
getVarNameGet the variable name at a given variable index.- Parameters:
- var- Index of the variable to look up.
- Returns:
- The name of the Stata variable.
 
- 
isAliasDetermine if a variable is an alias for a variable in another frame.- Parameters:
- var- Variable to access.
- Returns:
- True for alias variables.
 
- 
isParsedIfTrueDetermine if an observation for the if expression qualifier used when Java was invoked is true or false.- Parameters:
- obs- The observation to test.
- Returns:
- True when the if expression evaluates to true for the specified observation. When an if expression is not specified when Java was invoked, this function will return true.
 
- 
isVarlistSpecifiedDetermine if a varlist was specified when Java was invoked.- Returns:
- True if a varlist was specified when Java was invoked.
 
- 
isVarTypeStrTest if a variable is of type str.- Parameters:
- var- The index of the variable to test.
- Returns:
- True if the variable is of type str.
 
- 
isVarTypeStringTest if a variable's type is string.- Parameters:
- var- The index of the variable to test.
- Returns:
- True if the variable is a string variable of either type str or type strL.
 
- 
isVarTypeStrLTest if a variable is of type strL.- Parameters:
- var- The index of the variable to test.
- Returns:
- True if the variable is of type strL.
 
- 
makeVarNameAttempt to form a valid variable name from a string.- Parameters:
- s- Source string.
- retainCase- If set, the case will not be converted to lowercase.
- Returns:
- The new variable name. Returns null if a valid name was not created.
 
- 
mapParsedVarIndexMap the variable index from the parsed varlist. For example, if Java was invoked with three variables, loop over 1, 2, and 3. For each iteration, use this method to translate 1, 2, and 3 to the correct variable index within the dataset.- Parameters:
- var- Parsed variable index.
- Returns:
- The actual variable index in the dataset.
 
- 
readBytesRead a sequence of bytes from a strL.- Parameters:
- sc- The StrLConnector representing a strL.
- b- The buffer into which the data are read.
- Returns:
- The total number of bytes read into the buffer, or -1 if there are no more data because the end has been reached. May return a negative Stata return code if an error occurs.
- Throws:
- IOException- Throws an IOException if an error occurs.
 
- 
readBytes@Synchronized public static int readBytes(StrLConnector sc, byte[] b, int off, int len) throws IOException Read a sequence of bytes from a strL.- Parameters:
- sc- The StrLConnector representing a strL.
- b- The buffer into which the data are read.
- off- The start offset in the destination array b.
- len- The maximum number of bytes read.
- Returns:
- The total number of bytes read into the buffer, or -1 if there are no more data because the end has been reached. May return a negative Stata return code if an error occurs.
- Throws:
- IOException- Throws an IOException if an error occurs.
 
- 
renameVarRename a Stata variable.- Parameters:
- var- Index of the variable to rename.
- newname- New variable name.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
setObsTotalSet the number of observations in the current Stata dataset.- Parameters:
- obs- The number of observations to set.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
setVarFormatSet the format for a Stata variable.- Parameters:
- var- Index of the variable to format.
- format- New format.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
setVarLabelSet the label for a Stata variable.- Parameters:
- var- Index of the variable to label.
- label- New label.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
storeBytesStore a byte buffer to a strL. You do not need to callallocateStrLbefore using this method.- Parameters:
- sc- The StrLConnector representing a strL.
- bytes- Bytes to store.
- binary- Mark the data as binary.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
storeNumStore a numeric value in the current Stata dataset. Variable-type promotion happens automatically if the value you are storing is larger than what the variable can currently store.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- value- Value to store.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
storeNumFastStore a numeric value in the current Stata dataset. This method does not perform variable-type promotion and does not update the modified state of the data. To mark the dataset as changed, you should make a single call toupdateModified().- Parameters:
- var- Variable to access.
- obs- Observation to access.
- value- Value to store.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
storeStrStore a string value in the current Stata dataset; this method can be used to store str or strL data types. Variable-type promotion happens automatically if the string you are storing is longer than what the variable can currently store.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- value- Value to store.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
storeStrfStore a string value in the current Stata dataset; this method can be used to store str data types. Variable-type promotion happens automatically if the string you are storing is longer than what the variable can currently store. This method will not promote the type to a strL. If the string is longer than the maximum length of a str, then the string will be truncated.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- value- Value to store.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
storeStrfFastStore a string value in the current Stata dataset; this method can be used to store str data types. This method does not perform variable-type promotion and does not update the modified state of the data. To mark the dataset as changed, you should make a single call toupdateModified(). If the string is longer than the current storage length, then the string will be truncated.- Parameters:
- var- Variable to access.
- obs- Observation to access.
- value- Value to store.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
updateModifiedInform Stata that its data has been modified. Most methods automatically invoke this function as needed. Avoid calling this method from within a loop.
- 
writeBytesWrite a byte buffer to a strL; the strL must be allocated usingallocateStrLbefore calling this method. The buffer size may be smaller than the allocation size for the strL so that calling this method multiple times will write the data in chunks. The current position of each write will be automatically maintained. Writing beyond the allocation size is not permitted.- Parameters:
- sc- The StrLConnector representing a strL.
- b- The buffer holding the data to store.
- Returns:
- Return code from Stata; 0 if successful.
 
- 
writeBytesWritelenbytes from the specified byte buffer starting at offsetoffto a strL; the strL must be allocated usingallocateStrLbefore calling this method. The buffer size may be smaller than the allocation size for the strL so that calling this method multiple times will write the data in chunks. The current position of each write will be automatically maintained. Writing beyond the allocation size is not permitted.- Parameters:
- sc- The StrLConnector representing a strL.
- b- The buffer holding the data to store.
- off- The offset into the buffer.
- len- The number of bytes to write.
- Returns:
- Return code from Stata; 0 if successful.
 
 
-