Series Type |
Most of the RATS commands use data series. Because of the special nature of time series data, we have distinguished between a data series and a simple one-dimensional array of numbers.
When you use a new name, say in a DATA or SET instruction, you are creating a variable of type SERIES. Internally, a SERIES consists of some bookkeeping information, such as the defined length and a pointer to the data.
When you use a series, you are actually specifying not just the series itself, but also the range of data to use. This may not always be apparent, due to the use of default ranges.
When you do an ALLOCATE instruction, or do some other instruction which first creates a data series, you are setting the number of entries in the standard workspace. This is the length that RATS assumes you mean when you create further data series where the number of elements isn’t clear—most commonly when you do a SET without the start and end parameters. The standard workspace length isn’t a limit on how large a data series can get. For instance, if your program starts with
cal(q) 1960:1
allocate 2023:4
you can create a series with 10000 data points using
set sims 1 10000 = %ran(1.0)
stats sims
you just have to give an explicit range on the SET. Instructions which are driven by (potentially) complicated expressions such as SET and SSTATS will assume the default workspace length unless you override it. Instructions which take only series themselves as inputs (STATISTICS and LINREG, for instance) work off the defined length of the series they are given.
See Series workspace for more detail on how the workspace length is set and how you can change it if needed.
Every data series has a sequence number or "handle" as well as a name. This is a remnant of early versions of RATS, but we have retained it because series handles can still be useful in some cases. In general, though, you will probably find it easier to use arrays of series or the %S function for handling these kinds of repetitive tasks. Nonetheless, it is helpful to understand how series handles work.
The first series created in a RATS program is series number 1, the second is number 2, etc. Suppose your program begins with these lines:
calendar(q) 1980:1
data(format=rats) 1980:1 2017:4 gdp wages m1 price
GDP is series 1, WAGES series 2, M1 series 3 and PRICE series 4.
Anywhere RATS expects a series name, you can also use an integer value or INTEGER variable. When RATS expects a set of series, you can supply a list of integers, a set of INTEGER variables, or even VECTORS of INTEGERS. For example:
print / 1 2
prints series 1 and 2 (GDP and WAGES using the above example). If you execute the instruction
print / gdp to price
RATS interprets GDP TO PRICE as all series from 1 (GDP) to 4 (PRICE). Notice that the series numbers are based purely upon the order in which the series were created.
If you want to check the number associated with a series name, just use COMPUTE to set an integer equal to the series handle, then DISPLAY it:
compute sernum = gdp
?sernum
Be Careful With Series Numbers!
Using series numbers in places where RATS always expects a series type variable is easy. For example, series on a STATS instruction expects a SERIES type so
do i=1,3
statistics i
end do
produces statistics for series numbers 1, 2, and 3 as you would expect.
The same is true for any series parameter, such as on SET instructions. For example, if X is series 1 and Y is series 2, the following instructions are identical:
set y = 100 + .3*x
set 2 = 100 + .3*x
However, the same is not true on the right hand side of a SET, FRML, or COMPUTE instruction, where RATS accepts a variety of variable and expression types. So:
set 2 = 1
simply sets entries of series 2 equal to the constant value 1, not the values of series number 1. If you want RATS to treat an integer expression as a series number in such cases, you need to indicate this explicitly, using either lag notation (SET or FRML) or the type modifier [SERIES] (on SET, FRML, COMPUTE, DISPLAY, etc.). For example:
set y = 1{0}
set y = ([series]1)
To refer to a specific entry of a series referenced by number, use the type modifier in parentheses, followed immediately by the entry number in another set of parens:
compute test= ([series]1)(3)
RATS also allows you to set up arrays of series, which can be very helpful for automating tasks involving many series, or for simply keeping track of a large number of series, particularly when those series are related to each other in some manner. As a simple example, suppose you want to generate series containing the natural logs of a list of other series. You can do something like:
declare vector[series] loggdp(10)
dofor s = usdgdp to ukgdp
set loggdp(%doforpass) = log(s{0})
end dofor
The %S function provides another handy way to refer to or create series. You supply a LABEL variable or expression as the argument for the function. If a series with that label exists, the function returns the number of that series. Otherwise, it creates a new series, using the supplied label as the series name. So the above example could also be handled using:
dofor serlist = usgdp to ukgdp
set %s("log"+%l(serlist)) = log(serlist{0})
end do
The %L function returns the label of series, so we use it to get the label of the current GDP series, and prefix that with the text “LOG” to create the name for our new series. For example, the first series will be LOGUSGDP. Because the SERLIST index variable only contains the number of the current series, we use the lag notation inside the LOG function so that we get the log of the associated series, not the log of that integer value.
More on Creating Series
SERIES variables are almost always set from context (or using %S as shown above), but there are other ways to create them:
•The (optional and now rarely used) series parameter on ALLOCATE creates a block of series numbered 1 to series. The first series created after the ALLOCATE is series+1. Note that you may see programs which (incorrectly) use an ALLOCATE instruction with two “date” parameters. While that almost certainly isn’t the correct use of the first parameter in ALLOCATE, it generally will create no problems as all it does is to set aside some “numbered” series which will never get used in the remainder of the program.
•The SCRATCH and CLEAR instructions both create new series. SCRATCH creates a block of consecutively numbered series. CLEAR creates series from a list of names or numbers.
•DECLARE SERIES name(s) can also be used to create series. Unlike the other methods listed above, the declared series are not automatically defined over any range (they have length zero). You won’t be able to refer to a particular element of the series, or set an element using COMPUTE, until the series has been defined over the appropriate range, such as by setting it with a SET or CLEAR instruction.
Copyright © 2025 Thomas A. Doan