use string to reference scalar variable

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.

use string to reference scalar variable

Postby cap » Mon Aug 31, 2009 3:48 pm

Hello,

I have a pre-existing scalar variable, and I'd like to refer to it in a computation by using a string that is equal to the variable's name. How can I do this?


An example I thought would work would be something like this:

compute myvar = 10
declare string mystr
compute mystr = "my" + "var"
compute myvar2 = &mystr/2


But this produces an error message.


Thanks in advance,
-cap
cap
 
Posts: 10
Joined: Thu Aug 14, 2008 2:32 pm

Re: use string to reference scalar variable

Postby TomDoan » Thu Sep 03, 2009 1:18 pm

The & works only in a limited number of situations (where RATS is expecting a string, such as a file name). You can do something like what you want for series, with the %s function. Could you be more specific about what you're trying to do?
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm

Re: use string to reference scalar variable

Postby cap » Thu Sep 03, 2009 2:35 pm

In my program I am specifying the string name of a certain variable, and then the program computes a variety of tests on that variable, typically using a reference such as %s(&series_name).

But for one particular test, I need to divide two scalars. One of these scalars has a name that is patterned by having some prefix and then the variable name follows (and is not something that can be computed directly from the series). The set of these scalars are all simply hardcoded/written explicitly in my program.

So, when I want to do this computation, I need to specify the division as scalar1/scalar2, and I need to reference the string name of scalar2 as a combination of two other strings, something like

compute scalar2 = "prefix" + &series_name

which creates the name of the proper variable already defined explicitly in my code,
and then I need to do the division with something like

compute value = scalar1 / &scalar2


which would then use the correct value among the several that are defined in my code.




Any ideas?
cap
 
Posts: 10
Joined: Thu Aug 14, 2008 2:32 pm

Re: use string to reference scalar variable

Postby TomDoan » Thu Sep 03, 2009 4:09 pm

It looks like this will do what you want:

Code: Select all
open copy temp.src
display(unit=copy) "compute scalar2 = " prefix+series_name
close copy
source temp.src
TomDoan
 
Posts: 2720
Joined: Wed Nov 01, 2006 5:36 pm


Return to Help With Programming

Who is online

Users browsing this forum: No registered users and 1 guest