How do I use a string variable as an equation name?

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

How do I use a string variable as an equation name?

Unread post by macro »

I have code that runs a series of LINREG instructions in a loop, with different dependent variables each time. I want to construct an equation name using those series names (i.e. the labels of the series) and use it with the "define=" option in LINREG. Here is a simple example of what I'm trying to code:

Code: Select all

calendar(q) 1959 1
allocate 50 2015:1

open data nipa.csv
data(format=cdf, org=columns, dateform="m/d/y") 1959:1 2015:1 gdp pce unemp resinv
close data

dofor s = gdp pce resinv
    compute eq_name = %l(s) + "_eq"
    linreg(print) s / 
    # unemp{1 to 2} constant
end dofor s

group full_model gdp_eq>>gdp  pce_eq>>pce  resinv_eq>>resinv
However, this code fails with this error:

Code: Select all

dofor s = gdp pce resinv
(01.0139)     compute eq_name = %l(s) + "_eq"
(01.0165)     linreg(print, define = eq_name)<<<<
## SX22. Expected Type EQUATION, Got STRING Instead
>>>> s /     # unemp{1 to 2} constant
presumably because the eq_name variable contains a string, not an equation name. How can I reference the string contained in eq_name, e.g. "gdp_eq" as an equation name that I can pass into the "define=" option of LINREG (or any other command that optionally takes an equation name)?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How do I use a string variable as an equation name?

Unread post by TomDoan »

Use a HASH[EQUATION].

Code: Select all

dec hash[equation] myeqns
dofor s = gdp pce resinv
   linreg(define=myeqns(%l(s))) s
   # unemp{1 to 2} constant
end dofor s
group full_model myeqns("gdp")>>gdp  myeqns("pce")>>pce  myeqns("resinv")>>resinv
See https://estima.com/forum/viewtopic.php?f=32&t=2368 for a question with a similar answer.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

Re: How do I use a string variable as an equation name?

Unread post by macro »

That looks like a useful feature. Are there other options that work with versions before version 9?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How do I use a string variable as an equation name?

Unread post by TomDoan »

You would have to use a VECT[EQUATION] and use a counter variable to keep track of the "slot" for each variable:

Code: Select all

dec vect[equation] myeqns(3)
compute ifill=1
dofor s = gdp pce resinv
   linreg(define=myeqns(ifill)) s
   # unemp{1 to 2} constant
   compute ifill=ifill+1
end dofor s
group full_model myeqns(1)>>gdp  myeqns(2)>>pce  myeqns(3)>>resinv
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

Re: How do I use a string variable as an equation name?

Unread post by macro »

Running the last code sample gives me this error:

Code: Select all

group full_model myeqns(1)>>gdp  myeqns(2)>>pce  myeqns(3)>>resinv
## P3. Instruction GROUP Requires Parameter formula
Sporadically, it throws a different error or crashes RATS entirely:

Code: Select all

dec vect[equation] myeqns
compute ifill=1
dofor s = gdp pce resinv
(01.0066)    linreg(define=myeqns(ifill)) s
(01.0098)    # unemp{1 to 2} constant
(01.0164)    compute ifill=ifill+1
(01.0187) end dofor s
## CP16. Fatal Error - RATS Program Bug in Executing Compile
The Error Occurred At Location 7, Line 1 of loop/block
group full_model myeqns(1)>>gdp  myeqns(2)>>pce  myeqns(3)>>resinv
## P3. Instruction GROUP Requires Parameter formula
I get these errors (sometimes) using either WinRATS 8.0 Pro x86 or WinRATS 8.3 Pro Beta x86. I'm not able to test them in version 9 yet.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How do I use a string variable as an equation name?

Unread post by TomDoan »

myeqns wasn't dimensioned in the original post. Add the (3) onto the declare instruction.
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

Re: How do I use a string variable as an equation name?

Unread post by macro »

Thanks for the correction. That does exactly what I need, and since I'm using RATS 8.3, I'm able to use the HASH data type as well.
Post Reply