@val |

Number from a string.

Syntax: @val(arg[, fmt])

arg: string

fmt: (optional) numeric format string

Return: scalar

Returns the number that string arg represents. You may provide an optional numeric format string fmt.

In most cases, EViews will be able to convert your string into the corresponding numeric value without additional input. If EViews is unable to perform a conversion, it will return a missing (NA) value.

There are a few important conventions used in the conversion process:

• A leading “$” in the string will be ignored.

• Strings enclosed in “( )” or with a leading “–” will be treated as negative numbers. All other numeric strings, including those with a leading “+” will be treated as positive numbers. You may not have a leading “+” or “–” inside of the parentheses.

• A trailing “%” sign instructs EViews to treat the input string as a percentage—the resulting value will be divided by 100.

There are some situations where you must provide a numeric format string so that EViews can properly interpret your input. The syntax for the format string depends on the type of number the string represents.

Real-Value Formats

EViews will properly interpret non-delimited decimal and scientific notation numeric input strings as numbers.

If your string uses “,” to separate thousands, you should specify the “t” format string to remove “,” delimiters prior to conversion. If the string uses “.” to separate thousands, you should use “t..” to instruct EViews to remove “.” delimiters.

If your input string represents a number with suppressed decimal format, you should include a format string beginning with the letter “s”:

s.precision | suppressed decimal point format (precision determines the number of digits to the right of the decimal) |

EViews will divide the resulting number by 10 raised to the power of the specified precision. The “s” format specification may be followed by a “t.” or a “t..” specification if necessary.

Integer Formats

r | ratio (e.g., “30 1/5”). |

i | integer |

h | hexadecimal |

o | octal |

b | binary |

You should use the “r”, “h”, “o”, or “b” formats to indicate that your input is in the specified format. The “i” format is generally not necessary unless you wish to produce a missing value for a non-integer input string.

Examples

scalar num = @val("$1.23")

assigns the scalar NUM the numeric value 1.23.

series ser1 = @val("-$123.88")

returns the value -123.88.

scalar sperct = @val("478%")

divides the value by 100, setting the scalar SPERCT to 4.78.

scalar sratio = @val("(321 1/5)", "r")

sets the scalar SRATIO equal to -321.2

scalar shexa = @val("f01a", "h")

treats the string “f01a” as a hexadecimal number, converts it into the decimal equivalent, 61466, and assigns it to the scalar object SHEXA.

scalar sbin = @val("11110101", "b")

interprets the string “11110101” as a binary number, converts it into the decimal equivalent, 245, and assigns it to the scalar SBIN.

To ensure that a value is an integer, you may use the “i” option.

scalar sintna = @val("98.32", "i")

scalar sint = @val("96", "i")

SINTNA will contain a missing value NA since the input represents a non-integer value, while SINT is set to 96.

You may use @val to convert values in an svector into a vector. The matrix command,

vector v = @val(sv1)

converts the string values of svector SV1 to numeric values and returns the values in the svector V. If the vector V exists it will be sized to match the rows of SV1 and non-numeric strings will be converted to NA.

The series command

series x = @val(alpha1)

converts the string values in the alpha series ALPHA1 to numeric values and returns the values in the series X. Non-numeric strings will be converted to NA.

Format strings may be used to govern the conversion,

vector vbin = @val(svbin, "b")

interprets the strings in the svector SVBIN as binary numbers, converts it into their decimal equivalents and assigns it to the vector VBIN. If for example, SVBIN contained “110” “001” and “010”, the resultant VBIN will contain 6, 1, and 2.

Cross-references

See also
@str to express numbers as strings.