Why does RATS not recognize a series created in a loop?

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

Why does RATS not recognize a series created in a loop?

Unread post by macro »

This is a minimal working example of a larger code base I have. The code loops over two models, "a" and "b", and creates a placeholder variable for the dependent variable. This allows me to simply refer to "gdp_" or "cons_" in the code, instead of the specific series that varies depending on the model.

Code: Select all

calendar(q) 1947 1
allocate 50 2015:2

smpl 1950:1 2000:1

open data nipa.csv
    data(format=cdf, org=columns, dateform="m/d/y") 1947:1 2015:2 gdp cons gce gov unemp
close data

* compute logs
dofor s = gdp cons gce gov unemp
    compute [label] lbl = "l" + %l(s)
    set %s(lbl) = log(s{0})
    
    compute [label] dlbl = "dl" + %l(s)
    set %s(dlbl) = log(s{0}) - log(s{1})
end dofor s

/****
dofor s = gdp cons gce gov
    compute [label] lbl_ = %l(s) + "_"
    set %s(lbl_) = %na
end dofor s
****/

declare string model
dofor model = "a" "b"
    dofor s = gdp cons gce gov
        compute [label] lbl_ = %l(s) + "_"
        if model == "a" {
            compute [label] s_lbl = "L" + %l(s)
        }
        else if model == "b" {
            compute [label] s_lbl = "DL" + %l(s)
        }
        set %s(lbl_) = %s(s_lbl)
        
        labels %s(lbl_)
        # s_lbl
    end dofor s
    
    dofor s = gdp_ cons_ gce_ gov_
        linreg s
        # unemp{1 to 4}
    end dofor s
end dofor model
However, this code throws an error:

Code: Select all

(01.0389)     dofor s = gdp_ <<<<
## SX11. Identifier GDP_ is Not Recognizable. Incorrect Option Field or Parameter Order?
>>>>Is it possible you meant
  GDP
  LGDP
If the name isn't mistyped, it's possible that you have a poorly formatted instruction
Common errors are
 * a space before the ( in an option field
 * a missing space before = in a SET or FRML
 * a missing $ at the end of a long line which continues to the next
cons_ gce_ gov_
If I include the commented out portion

Code: Select all

dofor s = gdp cons gce gov
    compute [label] lbl_ = %l(s) + "_"
    set %s(lbl_) = %na
end dofor s
the code works properly. My intuition says that this happens because when RATS compiles the loop, it sees "gdp_", "cons_", etc. and can't determine that they're created dynamically in the program. Is this correct?

EDIT:
Furthermore, I actually tried simply declaring the series

Code: Select all

dofor s = gdp cons gce gov
    compute [label] lbl_ = %l(s) + "_"
    declare series %s(lbl_)
end dofor s
but this also throws an error

Code: Select all

(01.0113)     declare series %s(## SX1. Identifier %S is Already in use as a(n) FUNCTION
so I think I'm either declaring the series wrong (most likely) or RATS actually needs values in the series for its compiler to recognize it.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Why does RATS not recognize a series created in a loop?

Unread post by TomDoan »

First, yes. The fact that everything is in the outer DOFOR loop means that a dynamically created name won't be recognized during the original parse.

DECLARE won't let you use a computed variable name. However, you can use CLEAR to put the variable into play.

Code: Select all

dofor s = gdp cons gce gov
    compute [label] lbl_ = %l(s) + "_"
    clear %s(lbl_)
end dofor s
Post Reply