Command Reference : Strings and Dates : Strings
  
Strings
 
String Operators
String Concatenation Operator
String Relational Operators
String Ordering
String Comparison (with non-empty strings)
String Comparison (with empty strings)
String Lists
String Functions
String Information Functions
String Manipulation Functions
String Conversion Functions
String Vector Functions
Special Functions that Return Strings
Using Strings in EViews
String Variables
String Objects
String Vectors
Alpha Series
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.
Strings are used in EViews in a variety of places. “Using Strings in EViews” offers a brief overview.
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” and “Translating between Date Strings and Date Numbers”).
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 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)”.
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”).
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.
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-character, 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 precedes 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"
"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”). 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” 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.
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 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, was necessary to support the use of string missing values.
It is still possible to perform comparisons using the previous behavior. One approach is to use the special functions @eqna and @neqna for equality and strict inequality comparisons without propagating NAs (see “String Information Functions”). For example, you may use the expressions
@eqna(x, y)
@neqna(x, y)
so that blanks in string 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.
Similarly, 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 non-literal 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 a string value is an empty string. The @isempty function tests whether a string is empty. The relational equality test against the blank string literal “” is equivalent to this function.
String Lists
A string list is an ordinary string that is interpreted as a space delimited list of string elements. For example, the string
"Here I stand"
may be interpreted as containing three elements, the words “Here”, “I” and “stand”. Double quotes may be used to include multiword elements in a list. Bearing in mind that the quote is used as an escape character for including quotes in strings, the list
"""Chicken Marsala"" ""Beef Stew"" Hamburger"
contains three elements, the expressions “Chicken Marsala”, “Beef Stew”, and “Hamburger”. Notice how the escaped double quotes are used to group words into single list elements.
Interpreting a string as a list of elements allows us to make use of functions which operate on each element in the string, rather than on each character. These methods can be useful for string manipulation and pattern searching. For example, we may find the intersection, union, or cross of two string lists. Additionally, we may manipulate the elements of a string list and find the elements that match or do not match a given pattern. For example, the string list function
@wkeep("ABC ABCC AABC", "?B*")
uses the pattern “?B*” to filter the string list “ABC ABCC AABC”. Elements with a single character, followed by the character “B”, then followed by any number of other characters are kept, returning: “ABC ABCC”.
String Functions
EViews provides a number of functions that may either be used with strings, or return string values.
Functions that treat a string as a string list begin with a “w”. Some string functions have corresponding list functions with the same name, preceded by a “w”. For instance, @left returns the leftmost characters of a string, while @wleft returns the leftmost elements of a string list.
String Information Functions
The following is a brief summary of the basic functions that take strings as an argument and return a number. ( “String and Date Function Reference” offers a more detailed description.)
@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 keyword form of this function, @len, is also supported.
@wcount(str_list): returns an integer value for the number of elements in the string list str_list.
@wcount("I did not do it")
returns the value 5.
@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 specify the occurrence. If the requested 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, so
@mid("1.23415", @instr("1.23415", "34"))
returns “3415”.
@wfind(str_list, str_cmp): looks for the string str_cmp in the string list str_list, and returns the element position in the list or 0 if the string is not in the list.
@wfind("I did it", "did")
returns the value 2.
The @wfindnc function performs the same operation, but the comparison is not case-sensitive.
@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")
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.
@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. See “String Conversion Functions” and @val for details.
@dateval(str[, fmt]): converts the string representation of a date string, str, into a date number using the optional format string fmt. See “String Conversion Functions” and @dateval for details.
@dtoo(str): (Date TO Obs) converts the string representation of a date, str, into an observation value for the active workfile. 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”.
@wleft(str_list, int): returns a string containing the int elements at the left end of the string list str_list. If there are fewer than int elements, @wleft will return the entire string list.
@wleft("I did not do it", 3)
returns the string “I did not”.
@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”.
@wright(str_list, int): returns a string containing the int elements at the right end of a string list. If there are fewer than int elements, @wright will return the entire string.
@wright("I doubt that I did it", 3)
returns the string “I did it”.
@mid(str, 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”.
@wmid(str_list, int1[, int2]): returns the string consisting of the elements starting from position int1 in the string. By default, @wmid returns all remaining elements of the string, but you may specify the optional integer int2, indicating the number of elements to be returned.
@wmid("I doubt that I did it", 2, 3)
returns “doubt you did”.
@mid("I doubt that I did it", 4)
returns the string “I did it”.
@word(str_list, int): returns the int element of the string list.
@word("I doubt that I did it", 2)
returns the second element of the string, “doubt”.
@wordq(str_list, int): returns the int element of the string list, while preserving quotes.
@wordq("""Chicken Marsala"" ""Beef Stew""", 2)
returns the second element of the string, “Beef Stew”. The @word function would return the same elements, but would not include quotation marks in the string.
@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”.
@wkeep(str_list, "pattern_list"): returns the list of elements in str_list that match the string pattern pattern_list. The pattern_list is space delimited, and may be made up of any number of “?” (indicates any single character) or “*” (indicates any number of characters).
@wkeep("ABC DEF GHI JKL", "?B? D?? *I")
keeps the first three elements of the string list, returning the string “ABC DEF GHI”.
@wdrop(str_list, "pattern_list"): returns a string list, dropping elements in str_list that match the string pattern pattern_list. The pattern_list is space delimited, and may be made up of any number of “?” (indicates any single character) or “*” (indicates any number of characters).
@wdrop("ABC DEF GHI JKL", "?B? D?? *I")
drops the first three elements of the string list, returning the string “JKL”.
@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?”.
@wreplace(str_list, “src_pattern”, “replace_pattern”): returns the base string list str_list, with the replacement pattern replace_pattern substituted for the target pattern src_pattern. The pattern lists may be made up of any number of “?” (indicates any single character) or “*” (indicates any number of characters).
@wreplace("ABC AB", "*B*", "*X*")
replaces all instances of “B” with “X”, returning the string “AXC AX”.
@wreplace("ABC DDBC", "??B?", "??X?")
replaces all instances of “B” which have two leading characters and one following character, returning the string “ABC DDXC”.
@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. ")
returns the string “I doubt that I did it.”.
@upper(str): returns the upper case representation of the string str.
@upper("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”.
@addquotes(str): returns the string str with quotation marks added to the left and right. Given a string S1 that contains the unquoted text: I did not do it,
@addquotes(S1)
returns the quoted string “I did not do it”.
@stripquotes(str): returns the string str with quotation marks removed from the left and right. Given a string S1 that contains the text: “I did not do it”,
@stripquotes(S1)
returns the unquoted string: “I did not do it”.
@stripparens(str): returns the string str with parentheses removed from the left and right. Given a string S1 that contains the text: “(I did not do it)”,
@stripparens(S1)
returns the string: “I did not do it”.
@wintersect(str_list1, str_list2): returns the intersection of str_list1 and str_list2.
@wintersect("John and Greg won", "Mark won but Greg lost")
returns the string “won Greg”.
@wunion(str_list1, str_list2): returns the union of str_list1 and str_list2.
@wunion("ABC DEF", "ABC G H def")
returns the string “ABC DEF G H def”. Each new element is added to the string list, skipping elements that have already been added to the list.
@wunique(str_list): returns str_list with duplicate elements removed from the list.
@wunique("fr1 fr2 fr1")
returns the string “fr1 fr2”.
@wnotin(str_list1, str_list2): returns elements of str_list1 that are not in str_list2.
@wnotin("John and Greg won", "and Greg")
returns the string “John won”.
@wcross(str_list1, str_list2[, “pattern”]): returns str_list1 crossed with str_list2, according to the string pattern. The default pattern is “??”, which indicates that each element of str_list1 should be crossed individually with each element of str_list2.
@wcross("ABC DEF", "1 2 3", "?-?")
returns the string list “ABC-1 ABC-2 ABC-3 DEF-1 DEF-2 DEF-3”, inserting a dash (“‑”) between each crossed element as the “?-?” pattern indicates.
@winterleave(str_list1, str_list2[, count1, count2]): Interleaves str_list1 with str_list2, according to the pattern specified by count1 and count2. The default uses counts of one.
@winterleave("A B C", "1 2 3")
interleaves “A B C” with “1 2 3” to produce the string list “A 1 B 2 C 3”.
@wsort(str_list[,”D”]): Returns sorted elements of str_list. Use the “D” flag to sort in descending order.
@wsort("fq8 Fq8 xpr1", "D")
sorts the string in descending order: “xpr1 Fq8 fq8”.
@wdelim(str_list, "src_delim", "dest_delim"): returns a string list, replacing every appearance of the src_delim delimiter in str_list with a dest_delim delimiter. Delimiters must be single characters.
@wdelim("Arizona, California, Washington", ",", "-")
identifies the comma as the source delimiter and replaces each comma with a dash, returning the string “Arizona-California-Washington”.
String Conversion Functions
The following functions convert between numbers or date numbers and strings:
@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 the function reference entry @datestr and “Dates” for additional details on date numbers and date format strings.
@dateval(str[, fmt]): converts the string representation of a date string, 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 the function reference entry @dateval and “Dates” for discussion of date numbers and format strings.
@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 always be included for positive numbers. The resulting value after rounding is “+1544.4”.
See the function reference entry @str for a detailed description of the conversion rules and syntax, along with additional examples.
@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")
See the function reference entry @val for a detailed description of the conversion rules.
String Vector Functions
The following functions either take a string vector as an argument, or return a string vector:
@rows(str_vector): returns the number of rows in str_vector.
For a string vector SV01 with 10 rows,
@rows(sv01)
returns the integer 10.
@wsplit(str_list): returns a string vector containing the elements of str_list.
If the string list SS01 contains “A B C D E F”, then
@wsplit(ss01)
returns an untitled svector, placing an element of SS01 in each row. Row one of the svector contains “A”, row two contains “B”, etc.
@wjoin(svector): returns a space delimited list containing the elements of svector.
This is the inverse of the @wsplit function.
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 in an Alpha series, formatted using the date format string fmt. See “Special Date Functions” 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”.
@otods(int): (Obs TO Date in Sample): returns a string representation of the date associated with a single observation (counting from the start of the sample). Thus
@otods(2)
will return the date associated with the second observation in the current sample. Note that if int is negative, or is greater than the number of observations in the current sample, an empty string will be returned.
@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”.
You may also ask EViews to report information about objects in the current workfile or database, or a directory on your computer, in a form suitable for list processing:
@wlookup(“pattern_list”[, “object_type_list”]): Returns a string list of all objects in the workfile or database that satisfy the pattern_list and, optionally, the object_type_list. The pattern_list may be made up of any number of “?” (indicates any single character) or “*” (indicates any number of characters).
If a workfile contains a graph object named “GR01” and two series objects named “SR1” and “SER2”, then
@wlookup("?R?","series")
returns the string “SR1”.
@wdir(directory_str): returns a string list of all files in the directory directory_str. Note that this does not include other directories nested within directory_str.
@wdir("C:\Documents and Settings")
returns a string list containing the names of all files in the “C:\Documents and Settings” directory.
Lastly, all EViews objects have data members which return information about themselves in the form of a string. For example:
ser1.@updatetime
returns the last update time for the series SER1
eq1.@command
returns the full command line form of the estimation command.
For lists of the relevant data members see the individual object descriptions in “Object View and Procedure Reference”.
Using Strings in EViews
Strings in EViews are primarily used in four distinct contexts: string variables, string objects, string vectors, or Alpha series.
String Variables
A string variable is a temporary variable used in a program whose value is a string. String variables, which only exist during the time that your EViews program is executing, have names that begin with a “%” symbol. For example,
%value = "value in millions of u.s. dollars"
%armas = "ar(1) ar(2) ma(1) ma(2)"
are string variables declarations that may be used in program files.
See “String Variables” for extensive discussion of the role that these variables play in programming.
String Objects
A string object is an EViews workfile object that holds a string of text:
string lunch = "Apple Tuna Cookie"
string dinner = """Chicken Marsala"" ""Beef Stew"" Hamburger"
creates the string objects LUNCH and DINNER, each containing the corresponding string literal. Note that we have used the double quote character as an escape character for double quotes.
Since a string object is an EViews workfile object, we may open and display its views. A string object’s view may be switched between String and Word list views. The String view for DINNER displays the text as a single string,
"Chicken Marsala" "Beef Stew" Hamburger
while the Word list view breaks up the text by element,
"Chicken Marsala"
"Beef Stew"
Hamburger
with each element on a separate line.
We emphasize the important distinction that string objects are named objects in the workfile that may be saved with the workfile, while string variables are temporary variables that only exist while an EViews program is running. Thus, string objects have the advantage that they may be used interactively, while string variables may not be used outside of programs. String objects can, however, go out of scope when the active workfile page changes, while string variables are always in scope.
In all other respects, strings objects and string variables may be used interchangeably in programs. Either string object or string variables can be passed into subroutines for arguments declared as type string.
String Vectors
An svector, or string vector, is an EViews object that holds a string in each row of the vector. A string vector can be created by specifying the number of rows in the vector and providing a name:
svector(3) svec
If a length is not specified, a one row svector will be created.
An svector can be populated by assigning a string or string literal to each row:
svec(1) = "gdp cost total"
fills the first row of SVEC with the string “gdp cost total”. To assign the same string to all rows, omit the row number. The command
svec = "invalid"
will assign the string “invalid” to all rows of SVEC.
A multiple row svector may be populated using the @wsplit command, which creates a string vector from a string list. For example,
svector svec
string st = "gdp cost total"
svec = @wsplit(st)
creates the string vector SVEC of default length one and a string object ST containing “gdp cost total”. The @wsplit command creates a three element svector from the elements of ST, placing the string “gdp” in the first row of the string vector, the string “cost” in the second row, and the string “total” in the third row, and assigns it to SVEC, which is resized accordingly.
Similarly, an svector will shrink if assigned to a smaller svector. For example,
svector svec3 = @wsplit("First Middle Last")
svector(10) svec10
svec10 = svec3
first creates the three row svector SVEC3, then assigns the strings “First”, “Middle”, and “Last” to the first, second, and third rows, respectively. The third line creates a second ten row svector, SVEC10. When SVEC3 is assigned to SVEC10, its values are copied over and rows four through ten are removed from SVEC10.
An svector may also be filled by concatenating two strings or svectors. For instance,
svector s1 = @wsplit("A B C")
svector s2 = @wsplit("1 2 3")
svector ssvec = s1 + s2
creates two svectors S1 and S2, each with three rows. S1 contains the characters “A”, “B”, and “C”, while S2 contains “1”, “2”, and “3”. The third command creates the svector SSVEC and fills it with the concatenation of the other two svectors, producing “A1” on the first row, “B2” on the second row, and “C3” on the third row.
More generally, any operation that can be performed on a string may be performed on element of an svector. For example, given an svector whose first element contains the string “Hello World” and whose second element contains “Hi there world”, the element assignment statement
svector sv(3) = @left(sv(1),5) + " " + @mid(sv(2),4,5)
takes the left five characters of the first row (“Hello”), adds a space, concatenates five characters from the second row, starting at the fourth character (“there”), and assigns it to the third element of SV. Element three of SV now contains the string “Hello there”.
The row count of a string vector may be retrieved using the @rows command:
scalar sc = @rows(sv)
This is especially useful since svectors are dynamically resized when necessary.
Alpha Series
EViews has a special series type for holding string data. An alpha series object contains a set of observations on string values. Alpha series should be used when you wish to work with variables that contain alphanumeric data, such as names, addresses, and other text.
Alpha series are distinguished from string vectors primarily in that their length is tied to the length of the workfile.
See “Alpha Series” for discussion.