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");
SFIToolkit.displayln("{hline 13}{c +}{hline 57}");
for (int i = 1; i <= parsedVariables; i++) {
double sum = 0;
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
double mean = 0;
double stddev = 0;
long count = 0;
// get the real variable index for the ith parsed variable
int varIndex = Data.mapParsedVarIndex(i);
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 (Data.isValueMissing(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 (Data.isValueMissing(value)) {
continue ;
}
d2sum += Math.pow(value-mean,2);
}
stddev = Math.sqrt(d2sum/(count-1));
}
// write out the results
String out = String.format("%12s {c |}%11s",
Data.getVarName(varIndex),
SFIToolkit.formatValue(count, "%11.0gc"));
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,
int obs,
boolean bValueLabel)
Deprecated.
This method cannot access observations beyond 2^31-1.
Use
getFormattedValue(int, long, boolean)
instead. |
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()
Get a Stata missing value.
|
static double |
getNum(int var,
int obs)
Deprecated.
This method cannot access observations beyond 2^31-1.
Use
getNum(int, long) instead. |
static double |
getNum(int var,
long obs)
Read a numeric value from the current Stata dataset.
|
static int |
getObsCount()
Deprecated.
This method cannot get counts beyond 2^31-1.
Use
getObsTotal() instead. |
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 |
getParsedIn1()
Deprecated.
This method cannot get observations beyond 2^31-1.
Use
getObsParsedIn1() instead. |
static int |
getParsedIn2()
Deprecated.
This method cannot get observations beyond 2^31-1.
Use
getObsParsedIn2() instead. |
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,
int obs)
Deprecated.
This method cannot access observations beyond 2^31-1.
Use
getStr(int, long) instead. |
static String |
getStr(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(int obs)
Deprecated.
This method cannot access observations beyond 2^31-1.
Use
isParsedIfTrue(long) instead. |
static boolean |
isParsedIfTrue(long obs)
Determine if an observation for the if
expression qualifier used with javacall is true
or false.
|
static boolean |
isValueMissing(double value)
Test if a value is a Stata missing.
|
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 |
setObsCount(int obs)
Deprecated.
This method cannot set observations beyond 2^31-1.
Use
setObsTotal(long) instead. |
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,
int obs,
double value)
Deprecated.
This method cannot access observations beyond 2^31-1.
Use
storeNum(int, long, double) instead. |
static int |
storeNum(int var,
long obs,
double value)
Store a numeric value in the current Stata dataset.
|
static int |
storeStr(int var,
int obs,
String value)
Deprecated.
This method cannot access observations beyond 2^31-1.
Use
storeStr(int, long, String) instead. |
static int |
storeStr(int var,
long obs,
String value)
Store a string value in the current Stata dataset.
|
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
public static double getNum(int var, long obs)
var
- Variable to access.obs
- Observation to access.@Deprecated public static double getNum(int var, int obs)
getNum(int, long)
instead.var
- Variable to access.obs
- Observation to access.public static String getStr(int var, long obs)
var
- Variable to access.obs
- Observation to access.String
. Returns null if an error occurs.@Deprecated public static String getStr(int var, int obs)
getStr(int, long)
instead.var
- Variable to access.obs
- Observation to access.String
. Returns null if an error occurs.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
.@Deprecated public static String getFormattedValue(int var, int obs, boolean bValueLabel)
getFormattedValue(int, long, boolean)
instead.var
- Variable to access.obs
- Observation to access.bValueLabel
- Use the value label when available.String
.public static int storeNum(int var, long obs, double value)
var
- Variable to access.obs
- Observation to access.value
- Value to store.@Deprecated public static int storeNum(int var, int obs, double value)
storeNum(int, long, double)
instead.var
- Variable to access.obs
- Observation to access.value
- Value to store.public static int storeStr(int var, long obs, String value)
var
- Variable to access.obs
- Observation to access.value
- Value to store.@Deprecated public static int storeStr(int var, int obs, String value)
storeStr(int, long, String)
instead.var
- Variable to access.obs
- Observation to access.value
- Value to store.public static int getVarCount()
public static long getObsTotal()
@Deprecated public static int getObsCount()
getObsTotal()
instead.public static int setObsTotal(long obs)
obs
- The number of observations to set.@Deprecated public static int setObsCount(int obs)
setObsTotal(long)
instead.obs
- The number of observations to set.public static int getType(int var)
var
- Variable to access.TYPE_BYTE
, TYPE_INT
, TYPE_LONG
,
TYPE_FLOAT
, TYPE_DOUBLE
, TYPE_STR
,
or TYPE_STRL
.public static int getBestType(double value)
value
- The value to test.TYPE_BYTE
, TYPE_INT
, TYPE_LONG
,
TYPE_FLOAT
, or TYPE_DOUBLE
.public static int addVarDouble(String name)
name
- Name of the variable to be created.public static int addVarFloat(String name)
name
- Name of the variable to be created.public static int addVarLong(String name)
name
- Name of the variable to be created.public static int addVarInt(String name)
name
- Name of the variable to be created.public static int addVarByte(String name)
name
- Name of the variable to be created.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 getMaxStrLength()
,
then a variable of type strL will be created.public static int addVarStrL(String name)
name
- Name of the variable to be created.public static int getVarIndex(String varname)
varname
- Name of the variable.public static String getVarName(int var)
var
- Index of the variable to look up.public static int renameVar(int var, String newname)
var
- Index of the variable to rename.newname
- New variable name.public static String getVarLabel(int var)
var
- Index of the variable to look up.public static int setVarLabel(int var, String label)
var
- Index of the variable to label.label
- New label.public static String getVarFormat(int var)
var
- Index of the variable to look up.public static int setVarFormat(int var, String format)
var
- Index of the variable to format.format
- New format.public static int dropVar(int var)
var
- Variable to drop.public static String makeVarName(String s, boolean retainCase)
s
- Source string.retainCase
- If set, the case will not be converted to
lowercase.public static int getMaxStrLength()
public static int getMaxVars()
public static boolean isVarTypeString(int var)
var
- The index of the variable to test.public static boolean isVarTypeStr(int var)
var
- The index of the variable to test.public static boolean isVarTypeStrL(int var)
var
- The index of the variable to test.public static int getStrVarWidth(int var)
var
- The index of the variable to test.public static boolean isValueMissing(double value)
value
- The value to test.public static double getMissingValue()
public static Double getRealOfString(String s)
String
using Stata's real()
function.s
- The string to convert.public static long getObsParsedIn1()
@Deprecated public static int getParsedIn1()
getObsParsedIn1()
instead.public static long getObsParsedIn2()
@Deprecated public static int getParsedIn2()
getObsParsedIn2()
instead.public static boolean isVarlistSpecified()
public static int getParsedVarCount()
public static int mapParsedVarIndex(int var)
var
- Parsed variable index.public static boolean isParsedIfTrue(long obs)
obs
- The observation to test.@Deprecated public static boolean isParsedIfTrue(int obs)
isParsedIfTrue(long)
instead.obs
- The observation to test.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.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.public static int storeBytes(StrLConnector sc, byte[] bytes, boolean binary)
sc
- The StrLConnector representing a strL.bytes
- Bytes to store.binary
- Mark the data as binary.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.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.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.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.