ValueLabel (sfi.ValueLabel)

class sfi.ValueLabel

This class provides access to Stata’s value labels.

Method Summary

createLabel(name) Create a new value-label name.
getLabel(name, value) Get the label for a specified value-label value.
getLabels(name) Get the labels for a specified value-label name.
getNames() Get the names of all value labels in the current dataset.
getValueLabels(name) Get the values and labels for a specified value-label name.
getValues(name) Get the values associated with a single value-label name.
getVarValueLabel(var) Get the value-label name associated with a variable.
removeLabel(name) Remove a value-label name.
removeLabelValue(name, value) Remove a value-label value.
removeVarValueLabel(var) Remove a value-label name from a variable.
setLabelValue(name, value, label) Set a value and label for a value-label name.
setVarValueLabel(var, labelName) Set the value label for a variable.

Method Detail

static createLabel(name)

Create a new value-label name.

Parameters:name (str) – The new name.
static getLabel(name, value)

Get the label for a specified value-label value.

Parameters:
  • name (str) – The name of the value label.
  • value (int) – The value to look up.
Returns:

The label for the specified value-label value. Returns an empty string if the value-label value is not found.

Return type:

str

static getLabels(name)

Get the labels for a specified value-label name. Use this method in conjunction with getValues() to obtain two lists, where list indexes are used to look up each value-label pairing.

Parameters:name (str) – The name of the value label.
Returns:A list of the labels for the specified value-label name.
Return type:list
static getNames()

Get the names of all value labels in the current dataset.

Returns:A list of value-label names.
Return type:list
static getValueLabels(name)

Get the values and labels for a specified value-label name.

Parameters:name (str) – The name of the value label.
Returns:A dictionary containing the values and label pairings for the specified value-label name.
Return type:directory
static getValues(name)

Get the values associated with a single value-label name. Use this method in conjunction with getLabels() to obtain two lists, where list indexes are used to look up each value-label pairing.

Parameters:name (str) – The name of the value label.
Returns:A list of the values for the specified value-label name.
Return type:list
static getVarValueLabel(var)

Get the value-label name associated with a variable.

Parameters:var (str or int) – Name or index of the variable.
Returns:The value-label name associated with a variable. Returns an empty string if a value label is not associated with the specified variable.
Return type:str
Raises:ValueError – If var is not found or out of range.
static removeLabel(name)

Remove a value-label name.

Parameters:name (str) – The name of the value label to remove.
static removeLabelValue(name, value)

Remove a value-label value.

Parameters:
  • name (str) – The name of the value label.
  • value (int) – The value to remove.
static removeVarValueLabel(var)

Remove a value-label name from a variable.

Parameters:var (str or int) – Name or index of the variable.
Raises:ValueError – If var is not found or out of range.
static setLabelValue(name, value, label)

Set a value and label for a value-label name.

Parameters:
  • name (str) – The name of the value label.
  • value (int) – The value.
  • label (str) – The label.
static setVarValueLabel(var, labelName)

Set the value label for a variable.

Parameters:
  • var (str or int) – Name or index of the variable.
  • labelName (str) – The value-label name.
Raises:

ValueError – This error can be raised if:

  • var is not found or out of range.
  • var is a string variable.

Examples

The following provides a few quick examples illustrating how to use this class:

>>> from sfi import ValueLabel
>>> stata: sysuse auto, clear
(1978 Automobile Data)
>>> ValueLabel.getNames()
['origin']
>>> ValueLabel.createLabel('repair')
>>> ValueLabel.getNames()
['repair', 'origin']
>>> ValueLabel.setLabelValue('repair', 1, 'One')
>>> ValueLabel.setLabelValue('repair', 2, 'Two')
>>> ValueLabel.setLabelValue('repair', 3, 'Three')
>>> ValueLabel.setLabelValue('repair', 4, 'Four')
>>> ValueLabel.setLabelValue('repair', 5, 'Five')
>>> ValueLabel.getValueLabels('repair')
{1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five'}
>>> ValueLabel.setVarValueLabel('rep78', 'repair')
>>> ValueLabel.getVarValueLabel('rep78')
'repair'