Package com.stata.sfi
Class Frame
java.lang.Object
com.stata.sfi.Frame
This class provides access to Stata frames. Functionality is provided by
wrapping a Stata frame in a Java object of type
Frame,
which provides many methods for accessing the underlying Stata frame. If the
underlying frame is renamed from Stata, Mata, etc., then access to the frame
from its object will be lost. For more information about Stata frames, see
help frames in Stata.
All variable and observation numbering begins at 1 unless otherwise stated.
-
Method Summary
Modifier and TypeMethodDescriptionintaddVarByte(String varname) Add a variable of type byte to the frame.intaddVarDouble(String varname) Add a variable of type double to the frame.intaddVarFloat(String varname) Add a variable of type float to the frame.intAdd a variable of type int to the frame.intaddVarLong(String varname) Add a variable of type long to the frame.intAdd a variable of type str to the frame.intaddVarStrL(String varname) Add a variable of type strL to the frame.intallocateStrL(StrLConnector dsc, long size) Allocate a strL so that a buffer can be stored usingwriteBytes; the contents of the strL will not be initialized.intallocateStrL(StrLConnector dsc, long size, boolean binary) Allocate a strL so that a buffer can be stored usingwriteBytes; the contents of the strL will not be initialized.intSet theFrameas the current working frame in Stata.static FrameConnect to an existing frame in Stata and return a newFrameinstance that can be used to access it.static FrameCreate a new frame in Stata and return a newFrameinstance that can be used to access it.intdrop()Drop the frame in Stata.intdropVar(int var) Drop the variable at the specified variable index.static StringgetCWF()Utility method for getting the name of the current working frame in Stata.getFormattedValue(int var, long obs, boolean bValueLabel) Read a value from the frame, applying its display format.static StringgetFrameAt(int index) Utility method for getting the name of a Stata frame at a given index.static intUtility method for getting the number of frames in Stata.static String[]Utility method for getting the names of all frames in Stata.getName()Get the name of the frame.doublegetNum(int var, long obs) Read a numeric value from the frame.longGet the number of observations in the frame.getStr(int index, long obs) Read a string value from the frame.getStrf(int index, long obs) Read a string value from the frame.intgetStrVarWidth(int var) Get the width of a variable of type str.intgetType(int var) Get the numeric data type for the specified variable.intGet the number of variables in the frame.getVarFormat(int var) Get the format for a variable in the frame.intgetVarIndex(String varname) Look up the variable index for the specified variable name in the frame.getVarLabel(int var) Get the label for a variable in the frame.getVarName(int var) Get the variable name at a given variable index.booleanisAlias(int var) Determine if a variable is an alias for a variable in another frame.booleanisVarTypeStr(int var) Test if a variable is of type str.booleanisVarTypeString(int var) Test if a variable is string or numeric.booleanisVarTypeStrL(int var) Test if a variable is of type strL.intreadBytes(StrLConnector dsc, byte[] b) Read a sequence of bytes from a strL.intreadBytes(StrLConnector dsc, byte[] b, int off, int len) Read a sequence of bytes from a strL.intRename the frame in Stata.intRename a variable.intsetObsTotal(long obs) Set the number of observations in the frame.intsetVarFormat(int var, String format) Set the format for a variable in the frame.intsetVarLabel(int var, String label) Set the label for a variable in the frame.intstoreBytes(int var, long obs, byte[] bytes, boolean binary) Store a byte buffer to a strL in the frame.intstoreNum(int var, long obs, double value) Store a numeric value in the frame.intstoreNumFast(int var, long obs, double value) Store a numeric value in the frame.intStore a string value in the frame.intStore a string value in the frame.intstoreStrfFast(int var, long obs, String value) Store a string value in the frame.voidInform Stata that the frame's data have been modified.intwriteBytes(StrLConnector dsc, byte[] b) Write a byte buffer to a strL; the strL must be allocated usingallocateStrLbefore calling this method.intwriteBytes(StrLConnector dsc, 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.
-
Method Details
-
connect
Connect to an existing frame in Stata and return a newFrameinstance that can be used to access it.- Parameters:
name- Name of an existing Stata frame.- Returns:
- A
Framethat corresponds to the existing frame in Stata. - Throws:
FrameException- If the frame does not already exist in Stata or if connecting to the frame fails.
-
create
Create a new frame in Stata and return a newFrameinstance that can be used to access it.- Parameters:
name- Name of the Stata frame to create.- Returns:
- A new
Framethat corresponds to the new frame in Stata. - Throws:
FrameException- If the creation of the new frame in Stata fails.
-
getCWF
Utility method for getting the name of the current working frame in Stata.- Returns:
- The name of the current working frame.
-
getFrameAt
Utility method for getting the name of a Stata frame at a given index.- Parameters:
index- The index for a frame.- Returns:
- The name of the frame for the specified index.
-
getFrameCount
Utility method for getting the number of frames in Stata.- Returns:
- The number of frames.
-
getFrames
Utility method for getting the names of all frames in Stata.- Returns:
- The names of all frames.
-
addVarByte
Add a variable of type byte to the frame.- Parameters:
varname- Name of the variable to be created.- Returns:
- Return code from Stata; 0 if successful.
-
addVarDouble
Add a variable of type double to the frame.- Parameters:
varname- Name of the variable to be created.- Returns:
- Return code from Stata; 0 if successful.
-
addVarFloat
Add a variable of type float to the frame.- Parameters:
varname- Name of the variable to be created.- Returns:
- Return code from Stata; 0 if successful.
-
addVarInt
Add a variable of type int to the frame.- Parameters:
varname- Name of the variable to be created.- Returns:
- Return code from Stata; 0 if successful.
-
addVarLong
Add a variable of type long to the frame.- Parameters:
varname- Name of the variable to be created.- Returns:
- Return code from Stata; 0 if successful.
-
addVarStr
Add a variable of type str to the frame.- Parameters:
varname- Name of the variable to be created.length- Initial size of the variable. If the length is greater thanData.getMaxStrLength(), then a variable of type strL will be created.- Returns:
- Return code from Stata; 0 if successful.
-
addVarStrL
Add a variable of type strL to the frame.- Parameters:
varname- Name of the variable to be created.- Returns:
- Return code from Stata; 0 if successful.
-
allocateStrL
Allocate 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:
dsc- The StrLConnector representing a strL.size- The size in bytes.- Returns:
- Return code from Stata; 0 if successful.
- Throws:
IOException- Throws an IOException if an error occurs.
-
allocateStrL
@Synchronized public int allocateStrL(StrLConnector dsc, long size, boolean binary) throws IOException Allocate a strL so that a buffer can be stored usingwriteBytes; the contents of the strL will not be initialized.- Parameters:
dsc- 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 callstoreStr, where the encoding is automatically handled.- Returns:
- Return code from Stata; 0 if successful.
- Throws:
IOException- Throws an IOException if an error occurs.
-
changeToCWF
Set theFrameas the current working frame in Stata. The current working frame in Stata can be accessed usingDataif desired.- Returns:
- Return code from Stata; 0 if successful.
-
clone
Create a newFrameinstance by cloning the currentFrameand its contents. This results in a new frame in Stata.- Parameters:
newName- The name of the new frame to be created.- Returns:
- A
Framethat corresponds to the newly cloned frame in Stata. - Throws:
FrameException- If the cloning of the frame fails.
-
drop
Drop the frame in Stata. You may not drop a frame if it is the current working frame in Stata.- Returns:
- Return code from Stata; 0 if successful.
-
dropVar
Drop the variable at the specified variable index.- Parameters:
var- Variable to drop.- Returns:
- Return code from Stata; 0 if successful.
-
getFormattedValue
Read a value from the frame, 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.
-
getName
Get the name of the frame.- Returns:
- The name of the frame.
-
getNum
Read a numeric value from the frame.- Parameters:
var- Variable to access.obs- Observation to access.- Returns:
- The value.
-
getObsTotal
Get the number of observations in the frame.- Returns:
- The number of observations.
-
getStr
Read a string value from the frame. This method can be used to read str or strL data types.- Parameters:
index- Variable to access.obs- Observation to access.- Returns:
- The
String. Returns null if an error occurs.
-
getStrf
Read a string value from the frame. This method can be used to read str data types.- Parameters:
index- Variable to access.obs- Observation to access.- Returns:
- The
String. Returns null if an error occurs.
-
getStrVarWidth
Get 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.
-
getType
Get the numeric 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.
-
getVarCount
Get the number of variables in the frame.- Returns:
- The number of variables.
-
getVarFormat
Get the format for a variable in the frame.- Parameters:
var- Index of the variable to look up.- Returns:
- The variable's format.
-
getVarIndex
Look up the variable index for the specified variable name in the frame.- Parameters:
varname- Name of the variable.- Returns:
- The variable index.
-
getVarLabel
Get the label for a variable in the frame.- Parameters:
var- Index of the variable to look up.- Returns:
- The variable's label.
-
getVarName
Get the variable name at a given variable index.- Parameters:
var- Index of the variable to look up.- Returns:
- The name of the variable.
-
isAlias
Determine if a variable is an alias for a variable in another frame.- Parameters:
var- Variable to access.- Returns:
- True for alias variables.
-
isVarTypeStr
Test 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.
-
isVarTypeString
Test if a variable is string or numeric.- 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.
-
isVarTypeStrL
Test 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.
-
readBytes
Read a sequence of bytes from a strL.- Parameters:
dsc- 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 int readBytes(StrLConnector dsc, byte[] b, int off, int len) throws IOException Read a sequence of bytes from a strL.- Parameters:
dsc- 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.
-
rename
Rename the frame in Stata.- Parameters:
newName- Name of the new frame.- Returns:
- Return code from Stata; 0 if successful.
-
renameVar
Rename a variable.- Parameters:
var- Index of the variable to rename.newname- New variable name.- Returns:
- Return code from Stata; 0 if successful.
-
setObsTotal
Set the number of observations in the frame.- Parameters:
obs- The number of observations to set.- Returns:
- Return code from Stata; 0 if successful.
-
setVarFormat
Set the format for a variable in the frame.- Parameters:
var- Index of the variable to format.format- New format.- Returns:
- Return code from Stata; 0 if successful.
-
setVarLabel
Set the label for a variable in the frame.- Parameters:
var- Index of the variable to label.label- New label.- Returns:
- Return code from Stata; 0 if successful.
-
storeBytes
Store a byte buffer to a strL in the frame. You do not need to callallocateStrLbefore using this method.- Parameters:
var- Variable to access.obs- Observation to access.bytes- Bytes to store.binary- Mark the data as binary.- Returns:
- Return code from Stata; 0 if successful.
-
storeNum
Store a numeric value in the frame. 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.
-
storeNumFast
Store a numeric value in the frame. This method does not perform variable-type promotion and does not update the modified state of the data. To mark the frame 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.
-
storeStr
Store a string value in the frame. 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.
-
storeStrf
Store a string value in the frame. 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.
-
storeStrfFast
Store a string value in the frame. 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 frame 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.
-
updateModified
Inform Stata that the frame's data have been modified. Most methods automatically invoke this function as needed. Avoid calling this method from within a loop. -
writeBytes
Write 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:
dsc- The StrLConnector representing a strL.b- The buffer holding the data to store.- Returns:
- Return code from Stata; 0 if successful.
-
writeBytes
Writelenbytes 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:
dsc- 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.
-