Variables and Arguments in G7

Variables in G7 were introduced earlier for use in add files, fadd files, do loops, and elsewhere. This section provides additional details.

The treatment of variables in G7 are similar to the treatment in DOS batch files. Up to 99 variables typically may be passed to an add or fadd file, within a do loop or function, and so on. The first argument that is passed to a file is referenced by ‘%1’, the second by ‘%2’, …, and ‘%99’. The appearance of a percent symbol (%) within a G7 script always signifies that the following character or word should be interpreted as a variable, a keyword, or a function. In each case, when the variable, keyword, or function is encountered and evaluated, the value of the result effectively will be inserted into the script and subsequently processed by G7.

Variables

Variables in G7 were introduced earlier. All arguments are passed as text to the add file, do loop, or other routine. Variables are referenced as %1, …, %99.

Numerical Routines

%{}

Integers often are passed to an add file or function, and integers are used as indexes for a do loop. Sometimes a calculation is needed that is based on that integer, but some adjustment is needed. The %{} operation allows such calculations. Numerical variables and constants may be referenced within the brackets, and addition, subtraction, multiplication, and division of variables and constants is permitted, and sub-calculations may be surrounded by parentheses. For example, suppose we need to print vector data to a spreadsheet. Suppose the vector elements range from 1-10, and we want to print these series in rows 6-15. We could use the “xl vecwrite” routine or we could construct a do loop like the following:

do{ xl write A %{5 + %1} vec%1 2000 2010 }(1-10)

Note that for each iteration ‘%1’, element ‘%1’ is referenced in the “vec” vector, and that series is written to row ‘5+%1’ in the spreadsheet.

Another common use of %{} is to provide unambiguous specification of variables within a script. Recall that variables ‘%1’ through ‘%99’ are legal. However, there may be ambiguity between a two-digit variable and a single-digit variable that is followed by another digit that is not part of the variable. For example, if ‘%9’ is followed by a ‘9’, it looks the same as ‘%99’ and so G7 will attempt to fill in the 99-th variable definition. To remove such ambiguity, use the following approach:

f x = %{%9}9 # Variable 9, followed by '9'
f x = %99    # Variable 99

In some cases, strings may be expanded and other text also is allowed within %{}.

%floor(<number>)
%ceiling(<number>)
These function return the value of nearest integer, either rounding down or up, respectively.
%fabs(<number>)
This function returns the absolute value of the argument.
%log(<number>)
%exp(<number>)
These function return the logarithm of number or the exponential e to the number.
%min(<number>, <number>)
%max(<number>, <number>)
This function returns the minimum or maximum values of the two numbers provided.
%mod(<number>, <number>)

The modulus function returns the remainder of dividing its first argument by the second.

Keywords

Several keywords were introduced earlier. The following is a summary of keywords in G7. Availability of particular regression statistics depends on the type of regression performed.

%date:

Insert the date.

%time:

Insert the time.

%NARGS:

The number of arguments that currently are in scope.

%%:

Treat this as text, evaluated as a single ‘%’.

%fdate<X>:

Recover the first or last fdates setting, where X = {1,2}.

%gdate<X>:

Recover the first, middle, or last gdates setting, where X = {1,2,3}.

%tdate<X>:

Recover the first or last tdates setting, where X = {1,2}.

%lim<X>:

Recover the first, middle, or last limits setting, where X = {1,2,3}.

%title:

Recover the current graph title.

%subtitle:

Recover the current graph subtitle.

%vaxtitle:

Recover the current graph vertical axis title.

%ncoef:

Recover the number of coefficients in the last regression.

%beta<X>:

Recover the parameters from the last regression, X={1,…,ncoef}.

%mexval<X>:

Recover the mexvals from the last regression, X={1,…,ncoef}.

%see:

Recover the standard error of the estimate from the last regression.

%rsq:

Recover the r-square value from the last regression.

%rho:

Recover rho from the last regression.

%rbarsq:

Recover the r-bar-square value from the last regression.

%mape:

Recover the mean absolute percentage error from the last regression.

%dw:

Recover the Durbin-Watson statistic from the last regression.

%nobs:

Recover the number of observations in the last regression.

%obsinregression:

Recover the number of observations in the last regression.

%logl:

Recover the log likelihood statistic from the last regression.

%xls:

Recover spreadsheet cell contents.

%xldate:

Recover a spreadsheet date.

%xlgdate:

Recover a spreadsheet date in G7 date format.

%xlyear:

Recover the year component of a spreadsheet date.

%xlquarter:

Recover the quarter component of a spreadsheet date.

%xlmonth:

Recover the month component of a spreadsheet date.

%xlday:

Recover the day component of a spreadsheet date.

%mainfontname:

Recover the typeface setting of the main G7 window.

%mainfontsize:

Recover the type size of the main G7 window.

%mainfontcolor:

Recover the font color of the main G7 window.

%maincolor:

Recover the background color of the main G7 window.

%mainfontcharset:

Recover the character set setting of the main G7 window.

%mainautofontsize:

Recover the auto font size setting of the main G7 window.

%mainfontbold:

Recover the bold font setting of the main G7 window.

%mainfontitalic:

Recover the italic font setting of the main G7 window.

%mainfontunderline:

Recover the underline font setting of the main G7 window.

%mainfontstrikeout:

Recover the strikeout font setting of the main G7 window.

%autocomplete:

%edfontname:

Recover the typeface setting of the editor window.

%edfontsize:

Recover the type size of the editor window.

%edfontcolor:

Recover the font color of the editor window.

%edcolor:

Recover the background color of the editor window.

%edfontcharset:

Recover the character set setting of the editor window.

%edfontbold:

Recover the bold font setting of the editor window.

%edfontitalic:

Recover the italic font setting of the editor window.

%edfontunderline:

Recover the underline font setting of the editor window.

%edfontstrikeout:

Recover the strikeout font setting of the editor window.

%version:

Recover the G7 version number.

Functions

A variety of functions also are available. The following is a summary of such functions in G7.

%nseries( <bank_letter>)

This function returns the number of series in the specified bank.

%exists( <series_name>)

This function returns 1 (0) when the series is found (not found).

%sexists( <string_name>)

This function returns 1 (0) when the string name is found (not found).

%xlcol( <integer>)
%xlcol( <letters>)
This function maps positive digits to letters or letters to digits. Typically, <int> will be a variable (e.g. ‘%1’) that is assigned an integer value. For integer arguments greater than 26, the results will follow the convention for identifying Excel spreadsheet columns. The following is the mapping for the positive sequence of integers: A…Z AA … AZ BA … BZ ….
%getval(<expression>, <date>)

The value of expression in period date is retrieved. Any algebraic expression that is legal for the f command may be given, or a variable may be named either from the workspace or from the specified bank.

%if(<condition>, <if_true>, <if_false>)

Insert <if_true> when <condition> is true, and insert <if_false> otherwise. The specification of <condition> is identical to that of the if command. <if_true> and <if_false> either may be named strings or text within quotes.

%s(<string> [, <first>[, <last>]])

Insert the value of the named string <string>, where <string> previously was declared and defined with the str command. Optional parameters <first> and <last> indicate the positions of the first and last characters of a desired substring of <string>. <last> will be interpreted either as the character specified or as the end of the word when <last> exceeds the length of the word.

The following functions operate on strings. The strings either may named strings that were defined by the str command or they may be provided as text or a variable that is surrounded by quotation marks (e.g. “x1” or “%1”).

%strlen(<string>)

Calculates the length of string. This follows the standard C libraries.

%strcmp(<string1>, <string2>)

Compares <string1> and <string2>, with case sensitivity. Value is 0 if strings match, following the standard C libraries.

%strcmpi(<string1>, <string2>)

Compares <string1> and <string2>, without case sensitivity. Value is 0 if strings match, following the standards of the C libraries.

%strncmp(<string1>, <string2>,<N>)

Compares the first N characters of <string1> and <string2>, with case sensitivity. Value is 0 if the first N characters of the strings match, following the standards of the C libraries.

%strncmpi(<string1>, <string2>,<N>)

Compares the first N characters of <string1> and <string2>, without case sensitivity. Value is 0 if the first N characters of strings match, following the standards of the C libraries.

%strstr(<string1>, <string2>)

Looks for the first instance of <string2> within <string1>. If found, then return the portion of <string1> beginning at that point. Otherwise, return an empty string. This is similar to the standard C libraries.

%strnset(<string>, <N>)

Construct a string composed of <N> repetitions of string. The length of the resulting string is limited to 90 characters. This function loosely follows the standard C libraries.

%lower(<string>)
%upper(<string>)
%titlecase(<string>)
Returns a copy of string after converting it to lowercase letters, capital letters, or title case (where the first letter of each word is capitalized).
%trim(<string>[,<separators>])
%trimleft(<string>[,<separators>])
%trimright(<string>[,<separators>])
Returns a copy of string after removing extraneous characters from the beginning, end, or both ends of string. The default behavior is to remove whitespace and control characters. Other characters separators optionally may be given in the syntax of the %eliteral() routine.
%literal( “<string>” )

The %literal() function prevents the characters of string from modification by the G7 parsing engine. This allows character sequences to be passed through that otherwise might prompt an error or other undesirable action.

%eliteral( “<string>” )

The %literal() function is identical to the %literal() function except that it escapes the sequences ‘0’, ‘a’, ‘b’, ‘t’, ‘f’, ‘n’, ‘r’, and ‘\’, converting each to its corresponding ASCII code. For example, ‘t’ is converted to the ASCII code for the tab character.