Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Eviews5 / EViews5 / Docs / EViews 5 Command Ref.pdf
Скачиваний:
47
Добавлен:
23.03.2015
Размер:
5.23 Mб
Скачать

Chapter 7. Strings and Dates

Strings

An alphanumeric string is a set of characters containing alphabetic (“alpha”) and numeric characters, and in some cases symbols, found on a standard keyboard. Strings in EViews may include spaces and dashes, as well as single or double quote characters. Note also that EViews does not support unicode characters.

When entering alphanumeric values into EViews, you generally should enclose your characters in double quotes. The following are all examples of valid string input:

"John Q. Public"

"Ax$23!*jFg5"

"000-00-0000"

"(949)555-5555"

"11/27/2002"

"3.14159"

You should use the double quote character as an escape character for double quotes in a string. Simply enter two double quote characters to include the single double quote in the string:

"A double quote is given by entering two "" characters."

Bear in mind that strings are simply sequences of characters with no special interpretation. The string values “3.14159” and “11/27/2002” might, for example, be used to represent a number and a date, but as strings they have no such intrinsic interpretation. To provide such an interpretation, you must use the EViews tools for translating string values into numeric or date values (see “String Information Functions” on page 124 and “Translating between Date Strings and Date Numbers” on page 135 and).

Lastly, we note that the empty, or null, string (“”) has multiple interpretations in EViews. In settings where we employ strings as a building block for other strings, the null string is interpreted as a blank string with no additional meaning. If, for example, we concatenate two strings, one of which is empty, the resulting string will simply be the non-empty string.

In other settings, the null string is interpreted as a missing value. In settings where we use string values as a category, for example when performing categorizations, the null string is interpreted as both a blank string and a missing value. You may then

120—Chapter 7. Strings and Dates

choose to exclude or not exclude the missing value as a category when computing a tabulation using the string values. This designation of the null string as a missing value is recognized by a variety of views and procedures in EViews and may prove useful.

Likewise, when performing string comparisons using blank strings, EViews generally treats the blank string as a missing value. As with numeric comparisons involving missing values, comparisons involving missing values will often generate a missing value. We discuss this behavior in greater detail in our discussion of “String Comparison (with empty strings)” on page 123.

String Operators

The following operators are supported for strings: (1) concatenation—plus (“+”), and (2) relational—equal to (“=”), not equal to (“<>”), greater than (“>”), greater than or equal to (“>=”), less than (“<“), less than or equal to (“<=”).

String Concatenation Operator

Given two strings, concatenation creates a new string which contains the first string followed immediately by the second string. You may concatenate strings in EViews using the concatenation operator, “+”. For example,

"John " + "Q." + " Public" "3.14" + "159"

returns the strings

"John Q. Public" "3.14159"

Bear in mind that string concatenation is a simple operation that does not involve interpretation of strings as numbers or dates. Note in particular that the latter entry yields the concatenated string, “3.14159”, not the sum of the two numeric values, “162.14”. To obtain numeric results, you will first have to convert your strings into a number (see “String Information Functions” on page 124).

Lastly, we note that when concatenating strings, the empty string is interpreted as a blank string, not as a missing value. Thus, the expression

"Mary " + "" + "Smith"

yields

"Mary Smith"

since the middle string is interpreted as a blank.

Strings—121

String Relational Operators

The relational operators return a 1 if the comparison is true, and 0 if the comparison is false. In some cases, relational comparisons involving null strings will return a NA.

String Ordering

To determine the ordering of strings, EViews employs the region-specific collation order as supplied by the Windows operating system using the user’s regional settings. Central to the tasks of sorting or alphabetizing, the collation order is the culturally influenced order of characters in a particular language.

While we cannot possibly describe all of the region-specific collation order rules, we note a few basic concepts. First, all punctuation marks and other non alphanumeric characters, except for the hyphen and the apostrophe precede the alphanumeric symbols. The apostrophe and hyphen characters are treated distinctly so that “were” and “we’re” remain close in a sorted list. Second, the collation order is case specific, so that the character “a” precedes “A”. In addition, similar characters are kept close so that strings beginning with “a” are followed by strings beginning with “A”, ahead of strings beginning with “b” and “B”.

Typically, we determine the order of two strings by evaluating strings character-by-charac- ter, comparing pairs of corresponding characters in turn, until we find the first pair for which the strings differ. If, using the collation order, we determine the first character is less than the second character, we say that the first string is less than the second string and the second string is greater than the first. Two strings are said to be equal if they have the same number of identical characters.

If the two strings are identical in every character, but one of them is shorter than the other, then a comparison will indicate that the longer string is greater. A corollary of this statement is that the null string is less than or equal to all other strings.

The multi-character elements that arise in many languages are treated as single characters for purposes of comparison, and ordered using region-specific rules. For example, the “CH” and “LL” in Traditional Spanish are treated as unique characters that come between “C” and “L” and “M”, respectively.

String Comparison (with non-empty strings)

Having defined the notion of string ordering, we may readily describe the behavior of the relational operators for non-empty (non-missing) strings. The “=” (equal), “>=” (greater than or equal), and “<=” (less than or equal), “<>” (not equal), “>” (greater than), and “<” (less than) comparison operators return a 1 or a 0, depending on the result of the string comparison. To illustrate, the following (non region-specific) comparisons return the value 1,

"abc" = "abc"

122—Chapter 7. Strings and Dates

"abc" <> "def" "abc" <= "def" "abc" < "abcdefg" "ABc" > "ABC"

"abc def" > "abc 1ef"

while the following return a 0,

"AbC" = "abc" "abc" <> "abc" "aBc" >= "aB1"

"aBC" <= "a123" "abc" >= "abcdefg"

To compare portions of strings, you may use the functions @LEFT, @RIGHT, and @MID to extract the relevant part of the string (see “String Manipulation Functions” on page 125). The relational comparisons,

@left("abcdef", 3) = "abc"

@right("abcdef", 3) = "def"

@mid("abcdef", 2, 2) = "bc"

all return 1.

In normal settings, EViews will employ case-sensitive comparisons (see “Case-Sensitive String Comparison” on page 96 for settings that enable caseless element comparisons in programs). To perform a caseless comparison, you should convert the expressions to all uppercase, or all lowercase using the @UPPER, or @LOWER functions. The comparisons,

@upper("abc") = @upper("aBC")

@lower("ABC" = @lower("aBc")

both return 1.

To ignore leading and trailing spaces, you should use the @LTRIM, @RTRIM, and @TRIM functions remove the spaces prior to using the operator. The relational comparisons,

@ltrim(" abc") = "abc"

@ltrim(" abc") = @rtrim("abc ")

@trim(" abc ") = "abc"

all return 1.

Strings—123

String Comparison (with empty strings)

Generally speaking, the relational operators treat the empty string as a missing value and return the numeric missing value NA when applied to such a string. Suppose, for example that an observation in the alpha series X contains the string “Apple”, and the corresponding observation in the alpha series Y contains a blank string. All comparisons (“X=Y”, “X>Y”, “X>=Y”, “X<Y”, “X<=Y”, and “X<>Y”) will generate an NA for that observation since the Y value is treated as a missing value.

Note that this behavior differs from EViews 4.1 and earlier in which empty strings were treated as ordinary blank strings and not as a missing value. In these versions of EViews, the comparison operators always returned a 0 or a 1. The change in behavior, while regrettable, is necessary to support the use of string missing values.

It is still possible to perform comparisons using the previous methods. One approach is to use the special functions @EQNA and @NEQNA for performing equality and strict inequality comparisons without propagating NAs (see “String Information Functions” on page 124). For example, you may use the expressions

@eqna(x, y)

@neqna(x, y)

so that blanks in either X or Y are treated as ordinary string values. Using these two functions, the observation where X contains “Apple” and Y contains the “” will evaluate to 0 and 1, respectively instead of NA.

Alternatively, if you specify a relational expression involving a literal blank string, EViews will perform the test treating empty strings as ordinary string values. If, for example, you test

x = ""

or

x < ""

all of the string values in X will be tested against the string literal “”. You should contrast this behavior with the behavior for the tests “X=Y” and “X<Y” where blank values of X or Y result in an NA comparison.

Lastly, EViews provides a function for the strict purpose of testing whether or not a string value is an empty string. The @ISEMPTY function tests whether each element of the alpha series contains an empty string. The relational equality test against the blank string literal “” is equivalent to this function.

124—Chapter 7. Strings and Dates

String Functions

EViews provides a number of functions that may be used with strings or that return string values.

String Information Functions

The following is a brief summary of the basic functions that take strings as an argument and return a number.

• @LENGTH(str): returns an integer value for the length of the string str.

@length("I did not do it")

returns the value 15.

A shortened form of this function, @LEN, is also supported.

@INSTR(str1, str2[, int]): finds the starting position of the target string str2 in the base string str1. By default, the function returns the location of the first occurrence of str2 in str1. You may provide an optional integer int to change the occurrence. If the occurrence of the target string is not found, @INSTR will return a 0.

The returned integer is often used in conjunction with @MID to extract a portion of the original string.

@instr("1.23415", "34")

returns the value 4, since the substring “34” appears beginning in the fourth character of the base string.

@VAL(str[, fmt]): converts the string representation of a number, str, into a numeric value. If the string has any non-digit characters, the returned value is an NA. You may provide an optional numeric format string fmt.

@val("1.23415")

@ISEMPTY(str): tests for whether str is a blank string, returning a 1 if str is a null string, and 0 otherwise.

@isempty("1.23415")

returns a 0, while

@isempty("")

returns the value 1.

@EQNA(str1, str2): tests for equality of str1 and str2, treating null strings as ordinary blank strings, and not as missing values. Strings which test as equal return a 1, and 0 otherwise. For example,

@eqna("abc", "abc")

Strings—125

returns a 1, while

@eqna("", "def")

returns a 0.

@NEQNA(str1, str2): tests for inequality of str1 and str2, treating null strings as ordinary blank strings, and not as missing values. Strings which test as not equal return a 1, and 0 otherwise.

@neqna("abc", "abc")

returns a 0,

@neqna("", "def")

returns a 1.

@DATEVAL(str[, fmt]): converts the string representation of a date, str, into a date number using the optional format string fmt.

@dateval("12/1/1999", "mm/dd/yyyy")

will return the date number for December 1, 1999 (730088) while

@dateval("12/1/1999", "dd/mm/yyyy")

will return the date number for January 12, 1999 (729765). See “Dates” beginning on page 129 for discussion of date numbers and format strings.

@DTOO(str): (Date TO Obs) converts the string representation of a date, str, into an observation value. Returns the scalar offset from the beginning of the workfile associated with the observation given by the date string. The string must be a valid EViews date.

create d 2/1/90 12/31/95 %date = "1/1/93"

!t = @dtoo(%date)

returns the value !T=762.

Note that @DTOO will generate an error if used in a panel structured workfile.

String Manipulation Functions

The following is a brief summary of the basic functions that take strings as an argument and return a string.

@LEFT(str, int): returns a string containing the int characters at the left end of the string str. If there are fewer than int characters, @LEFT will return the entire string.

@left("I did not do it", 5)

returns the string “I did”.

126—Chapter 7. Strings and Dates

@RIGHT(str, int): returns a string containing the int characters at the right end of a string. If there are fewer than int characters, @RIGHT will return the entire string.

@right("I doubt that I did it", 8)

returns the string “I did it”.

@MID(str1, int1[, int2]): returns the string consisting of the characters starting from position int1 in the string. By default, @MID returns the remainder of the string, but you may specify the optional integer int2, indicating the number of characters to be returned.

@mid("I doubt that I did it", 9, 10)

returns “that I did”.

@mid("I doubt that I did it", 9)

returns the string “that I did it”.

@INSERT(str1, str2, int): inserts the string str2 into the base string str1 at the position given by the integer int.

@insert("I believe it can be done", "not ", 16)

returns “I believe it cannot be done”.

@REPLACE(str1, str2, str3[, int]): returns the base string str1, with the replacement str3 substituted for the target string str2. By default, all occurrences of str2 will be replaced, but you may provide an optional integer int to specify the number of occurrences to be replaced.

@replace("Do you think that you can do it?", "you", "I")

returns the string “Do I think that I can do it?”, while

@replace("Do you think that you can do it?", "you", "I", 1)

returns “Do I think that you can do it?”.

• @LTRIM(str): returns the string str with spaces trimmed from the left.

@ltrim(" I doubt that I did it. ")

returns “I doubt that I did it. ”. Note that the spaces on the right remain.

• @RTRIM(str): returns the string str with spaces trimmed from the right.

@rtrim(" I doubt that I did it. ")

returns the string “ I doubt that I did it.”. Note that the spaces on the left remain.

@TRIM(str): returns the string str with spaces trimmed from the both the left and the right.

@trim(" I doubt that I did it. ")

Strings—127

returns the string “I doubt that I did it.”.

• @UPPER(str): returns the upper case representation of the string str.

@length("I did not do it")

returns the string “I DID NOT DO IT”.

• @LOWER(str): returns the lower case representation of the string str.

@lower("I did not do it")

returns the string “i did not do it”.

String Conversion Functions

The following functions convert numbers and date numbers into string values:

@STR(num[, fmt]): returns a string representation of the number num. You may provide an optional numeric format string fmt.

@str(153.4)

returns the string “153.4”.

To create a string containing 4 significant digits and leading “$” character, use

@str(-15.4435,"g$.4")

The resulting string is “-$15.44”.

The expression

@str(-15.4435,"f7..2")

converts the numerical value, -15.4435, into a fixed 7 character wide decimal string with 2 digits after the decimal and comma as decimal point. The resulting string is “ -15,44”. Note that there is a leading space in front of the “-” character making the string 7 characters long.

The expression

@str(-15.4435,"e(..2)")

converts the numerical value, -15.4435, into a string written in scientific notation with two digits to the right of the decimal point. The decimal point in the value will be represented using a comma and negative numbers will be enclosed in parenthesis. The resulting string is “(1,54e+01)”. A positive value will not have the parenthesis.

@str(15.4435,"p+.1")

converts the numeric value, 15.4435, into a percentage where the value is multiplied by 100. Only 1 digit will be included after the decimal and an explicit “+” will

128—Chapter 7. Strings and Dates

always be included for positive numbers. The resulting value after rounding is “+1544.4”.

@DATESTR(date1[, fmt]): converts the date number date1 to a string representation using the optional date format string, fmt.

@datestr(730088,"mm/dd/yy")

will return “12/1/99”,

@datestr(730088,"DD/mm/yyyy")

will return “01/12/1999”, and

@datestr(730088,"Month dd, yyyy")

will return “December 1, 1999”, and

@datestr(730088,"w")

will produce the string “3”, representing the weekday number for December 1, 1999.

See “Dates” on page 129 for additional details on date numbers and date format strings.

Special Functions that return Strings

EViews provides a special, workfile-based function that uses the structure of the active workfile page and returns a set of string values representing the date identifiers associated with the observations.

@STRDATE(fmt): returns the set of workfile row dates as strings, using the date format string fmt. See “Special Date Functions” on page 147 for details.

In addition, EViews provides two special functions that return a string representations of the date associated with a specific observation in the workfile, or with the current time.

@OTOD(int): (Obs TO Date): returns a string representation of the date associated with a single observation (counting from the start of the workfile). Suppose, for example, that we have a quarterly workfile ranging from 1950Q1 to 1990Q4. Then

@otod(16)

returns the date associated with the 16th observation in the workfile in string form, “1953Q4”.

@STRNOW(fmt): returns a string representation of the current date number (at the moment the function is evaluated) using the date format string, fmt.

@strnow("DD/mm/yyyy")

returns the date associated with the current time in string form with 2-digit days, months, and 4-digit years separated by a slash, “24/12/2003”.

Соседние файлы в папке Docs