use string to reference scalar variable
use string to reference scalar variable
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
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
Re: use string to reference scalar variable
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?
Re: use string to reference scalar variable
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?
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?
Re: use string to reference scalar variable
It looks like this will do what you want:
With RATS 8.3, you could do this using a HASH data type for the scalars.
Code: Select all
open copy temp.src
display(unit=copy) "compute scalar2 = " prefix+series_name
close copy
source temp.srcCode: Select all
open data oecdsample.rat
calendar(q) 1981
data(format=rats) 1981:1 2006:4 can3mthpcp canexpgdpchs canexpgdpds canm1s canusxsr usaexpgdpch
*
set logcangdp = log(canexpgdpchs)
set logcandefl = log(canexpgdpds)
set logcanm1 = log(canm1s)
set logusagdp = log(usaexpgdpch)
set logexrate = log(canusxsr)
*
dec hash[real] vs
compute vs("can3mthpcp")=100.0
compute vs("logcangdp")=1.0
compute vs("logcandefl")=10.0
compute vs("logcanm1")=1000.0
compute vs("logusagdp")=10.0
compute vs("logexrate")=1.0
*
dofor s = can3mthpcp logcangdp to logexrate
stats(noprint) s
disp "Test calculation" %l(s) %mean/vs(%l(s))
end dofor s
Last bumped by TomDoan on Tue Dec 24, 2013 12:50 pm.