public final class Data extends Object
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 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;
}
. sysuse auto, clear
(1978 Automobile Data)
. 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
Modifier and Type | Field and Description |
---|---|
static int |
TYPE_BYTE |
static int |
TYPE_DOUBLE |
static int |
TYPE_FLOAT |
static int |
TYPE_INT |
static int |
TYPE_LONG |
static int |
TYPE_STR |
static int |
TYPE_STRL |
Modifier and Type | Method and Description |
---|---|
static int |
addVarByte(String name)
Add a variable of type byte to the current Stata dataset.
|
static int |
addVarDouble(String name)
Add a variable of type double to the current Stata dataset.
|
static int |
addVarFloat(String name)
Add a variable of type float to the current Stata dataset.
|
static int |
addVarInt(String name)
Add a variable of type int to the current Stata dataset.
|
static int |
addVarLong(String name)
Add a variable of type long to the current Stata dataset.
|
static int |
addVarStr(String name,
int length)
Add a variable of type str to the current Stata dataset.
|
static int |
addVarStrL(String name)
Add a variable of type strL to the current Stata dataset.
|
static int |
allocateStrL(StrLConnector sc,
long size)
Allocate a strL so that a buffer can be stored using
writeBytes ; the contents
of the strL will not be initialized. |
static int |
allocateStrL(StrLConnector sc,
long size,
boolean binary)
Allocate a strL so that a buffer can be stored using
writeBytes ; the contents
of the strL will not be initialized. |
static int |
dropVar(int var)
Drop the variable at the specified variable index.
|
static int |
getBestType(double value)
Get the best numeric data type for the specified value.
|
static String |
getFormattedValue(int var,
long obs,
boolean bValueLabel)
Read a value from the current Stata dataset, applying its display format.
|
static int |
getMaxStrLength()
Get the maximum length of a Stata string variable of type str.
|
static int |
getMaxVars()
Get the maximum number of variables Stata currently allows.
|
static double |
getMissingValue()
Deprecated.
Missing.getValue() should be used instead. |
static double |
getNum(int var,
long obs)
Read a numeric value from the current Stata dataset.
|
static long |
getObsParsedIn1()
Get the first in a range of observations if javacall was called
with the in qualifier.
|
static long |
getObsParsedIn2()
Get the last in a range of observations if javacall was called
with the in qualifier.
|
static long |
getObsTotal()
Get the number of observations in the current Stata dataset.
|
static int |
getParsedVarCount()
Get the number of variables specified when javacall was called.
|
static Double |
getRealOfString(String s)
Get the double representation of a
String using Stata's
real() function. |
static String |
getStr(int var,
long obs)
Read a string value from the current Stata dataset.
|
static String |
getStrf(int var,
long obs)
Read a string value from the current Stata dataset.
|
static int |
getStrVarWidth(int var)
Get the width of a variable of type str.
|
static int |
getType(int var)
Get the data type for the specified variable.
|
static int |
getVarCount()
Get the number of variables in the current Stata dataset.
|
static String |
getVarFormat(int var)
Get the format for a Stata variable.
|
static int |
getVarIndex(String varname)
Look up the variable index for the specified name in the current Stata
dataset.
|
static String |
getVarLabel(int var)
Get the label for a Stata variable.
|
static String |
getVarName(int var)
Get the variable name at a given variable index.
|
static boolean |
isParsedIfTrue(long obs)
Determine if an observation for the if expression qualifier used
with javacall is true or false.
|
static boolean |
isStataMissing(String s)
Deprecated.
Missing.parseIsMissing(String) should be used
instead. |
static boolean |
isValueMissing(double value)
Deprecated.
Missing.isMissing(double) should be used instead. |
static boolean |
isVarlistSpecified()
Determine if a varlist was specified with javacall.
|
static boolean |
isVarTypeStr(int var)
Test if a variable is of type str.
|
static boolean |
isVarTypeString(int var)
Test if a variable's type is string.
|
static boolean |
isVarTypeStrL(int var)
Test if a variable is of type strL.
|
static String |
makeVarName(String s,
boolean retainCase)
Attempt to form a valid variable name from a string.
|
static int |
mapParsedVarIndex(int var)
Map the variable index from the parsed varlist.
|
static int |
readBytes(StrLConnector sc,
byte[] b)
Read a sequence of bytes from a strL in the current Stata dataset.
|
static int |
readBytes(StrLConnector sc,
byte[] b,
int off,
int len)
Read a sequence of bytes from a strL in the current Stata dataset.
|
static int |
renameVar(int var,
String newname)
Rename a Stata variable.
|
static int |
setObsTotal(long obs)
Set the number of observations in the current Stata dataset.
|
static int |
setVarFormat(int var,
String format)
Set the format for a Stata variable.
|
static int |
setVarLabel(int var,
String label)
Set the label for a Stata variable.
|
static int |
storeBytes(StrLConnector sc,
byte[] bytes,
boolean binary)
Store a byte buffer to a strL in the current Stata dataset.
|
static int |
storeNum(int var,
long obs,
double value)
Store a numeric value in the current Stata dataset.
|
static int |
storeNumFast(int var,
long obs,
double value)
Store a numeric value in the current Stata dataset.
|
static int |
storeStr(int var,
long obs,
String value)
Store a string value in the current Stata dataset.
|
static int |
storeStrf(int var,
long obs,
String value)
Store a string value in the current Stata dataset.
|
static int |
storeStrfFast(int var,
long obs,
String value)
Store a string value in the current Stata dataset.
|
static void |
updateModified()
Inform Stata that its data has been modified.
|
static int |
writeBytes(StrLConnector sc,
byte[] b)
Write a byte buffer to a strL in the current Stata dataset; the
strL must be allocated using
allocateStrL before
calling this method. |
static int |
writeBytes(StrLConnector sc,
byte[] b,
int off,
int len)
Write
len bytes from the specified byte buffer starting at
offset off to a strL in the current Stata dataset;
the strL must be allocated using
allocateStrL before
calling this method. |
public static final int TYPE_BYTE
public static final int TYPE_INT
public static final int TYPE_LONG
public static final int TYPE_FLOAT
public static final int TYPE_DOUBLE
public static final int TYPE_STR
public static final int TYPE_STRL
@Synchronized public static int addVarByte(String name)
name
- Name of the variable to be created.@Synchronized public static int addVarDouble(String name)
name
- Name of the variable to be created.@Synchronized public static int addVarFloat(String name)
name
- Name of the variable to be created.@Synchronized public static int addVarInt(String name)
name
- Name of the variable to be created.@Synchronized public static int addVarLong(String name)
name
- Name of the variable to be created.@Synchronized public static int addVarStr(String name, int length)
name
- Name of the variable to be created.length
- Initial size of the variable. If the length is greater than
Data.getMaxStrLength()
, then a variable of type
strL will be created.@Synchronized public static int addVarStrL(String name)
name
- Name of the variable to be created.@Synchronized public static int allocateStrL(StrLConnector sc, long size)
writeBytes
; the contents
of the strL will not be initialized. By default, the data will be
marked as binary.sc
- The StrLConnector representing a strL.size
- The size in bytes.@Synchronized public static int allocateStrL(StrLConnector sc, long size, boolean binary)
writeBytes
; the contents
of the strL will not be initialized.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.@Synchronized public static int dropVar(int var)
var
- Variable to drop.@ThreadSafe public static int getBestType(double value)
value
- The value to test.Data.TYPE_BYTE
, Data.TYPE_INT
, Data.TYPE_LONG
,
Data.TYPE_FLOAT
, or Data.TYPE_DOUBLE
.@Synchronized public static String getFormattedValue(int var, long obs, boolean bValueLabel)
var
- Variable to access.obs
- Observation to access.bValueLabel
- Use the value label when available.String
.@ThreadSafe public static int getMaxStrLength()
@ThreadSafe public static int getMaxVars()
@Deprecated @ThreadSafe public static double getMissingValue()
Missing.getValue()
should be used instead.@ThreadSafe public static double getNum(int var, long obs)
var
- Variable to access.obs
- Observation to access.@Synchronized public static long getObsParsedIn1()
@Synchronized public static long getObsParsedIn2()
@ThreadSafe public static long getObsTotal()
@Synchronized public static int getParsedVarCount()
@ThreadSafe public static Double getRealOfString(String s)
String
using Stata's
real() function.s
- The string to convert.@Synchronized public static String getStr(int var, long obs)
var
- Variable to access.obs
- Observation to access.String
. Returns null if an error occurs.@ThreadSafe public static String getStrf(int var, long obs)
var
- Variable to access.obs
- Observation to access.String
. Returns null if an error occurs.@ThreadSafe public static int getStrVarWidth(int var)
var
- The index of the variable to test.@ThreadSafe public static int getType(int var)
var
- Variable to access.Data.TYPE_BYTE
,
Data.TYPE_INT
, Data.TYPE_LONG
, Data.TYPE_FLOAT
,
Data.TYPE_DOUBLE
, Data.TYPE_STR
, or Data.TYPE_STRL
.@ThreadSafe public static int getVarCount()
@ThreadSafe public static String getVarFormat(int var)
var
- Index of the variable to look up.@Synchronized public static int getVarIndex(String varname)
varname
- Name of the variable.@ThreadSafe public static String getVarLabel(int var)
var
- Index of the variable to look up.@ThreadSafe public static String getVarName(int var)
var
- Index of the variable to look up.@ThreadSafe public static boolean isParsedIfTrue(long obs)
obs
- The observation to test.@Deprecated @ThreadSafe public static boolean isStataMissing(String s)
Missing.parseIsMissing(String)
should be used
instead.s
- The value to test.@Deprecated @ThreadSafe public static boolean isValueMissing(double value)
Missing.isMissing(double)
should be used instead.value
- The value to test.@Synchronized public static boolean isVarlistSpecified()
@ThreadSafe public static boolean isVarTypeStr(int var)
var
- The index of the variable to test.@ThreadSafe public static boolean isVarTypeString(int var)
var
- The index of the variable to test.@ThreadSafe public static boolean isVarTypeStrL(int var)
var
- The index of the variable to test.@ThreadSafe public static String makeVarName(String s, boolean retainCase)
s
- Source string.retainCase
- If set, the case will not be converted to lowercase.@ThreadSafe public static int mapParsedVarIndex(int var)
var
- Parsed variable index.@Synchronized public static int readBytes(StrLConnector sc, byte[] b) throws IOException
sc
- The StrLConnector representing a strL.b
- The buffer into which the data are read.IOException
- Throws an IOException if an error occurs.@Synchronized public static int readBytes(StrLConnector sc, byte[] b, int off, int len) throws IOException
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.IOException
- Throws an IOException if an error occurs.@Synchronized public static int renameVar(int var, String newname)
var
- Index of the variable to rename.newname
- New variable name.@Synchronized public static int setObsTotal(long obs)
obs
- The number of observations to set.@Synchronized public static int setVarFormat(int var, String format)
var
- Index of the variable to format.format
- New format.@Synchronized public static int setVarLabel(int var, String label)
var
- Index of the variable to label.label
- New label.@Synchronized public static int storeBytes(StrLConnector sc, byte[] bytes, boolean binary)
allocateStrL
before using this method.sc
- The StrLConnector representing a strL.bytes
- Bytes to store.binary
- Mark the data as binary.@Synchronized public static int storeNum(int var, long obs, double value)
var
- Variable to access.obs
- Observation to access.value
- Value to store.@ThreadSafe public static int storeNumFast(int var, long obs, double value)
Data.updateModified()
.var
- Variable to access.obs
- Observation to access.value
- Value to store.@Synchronized public static int storeStr(int var, long obs, String value)
var
- Variable to access.obs
- Observation to access.value
- Value to store.@Synchronized public static int storeStrf(int var, long obs, String value)
var
- Variable to access.obs
- Observation to access.value
- Value to store.@ThreadSafe public static int storeStrfFast(int var, long obs, String value)
Data.updateModified()
. If the string is longer than the current
storage length, then the string will be truncated.var
- Variable to access.obs
- Observation to access.value
- Value to store.@Synchronized public static void updateModified()
@Synchronized public static int writeBytes(StrLConnector sc, byte[] b)
allocateStrL
before
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.sc
- The StrLConnector representing a strL.b
- The buffer holding the data to store.@Synchronized public static int writeBytes(StrLConnector sc, byte[] b, int off, int len)
len
bytes from the specified byte buffer starting at
offset off
to a strL in the current Stata dataset;
the strL must be allocated using
allocateStrL
before
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.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.