Hi,
I have got a short question. Is there any way in Rats to automatically generate name of a series?
More precisely, I need to add a string variable (Country) to the name of a series. Having written a procedure where the string variable country is "Germany" and the name of the series is "tu12" . I would like to name the series automatically "tu12Germany" since it is overwritten if the procedure is carried out for another country like Japan. Hence, the procedure should automatically edit tu12Germany to tu12Japan etc.
Thanks for your help
Generate name of a series
Re: Generate name of a series
You can certainly generate series names or labels (and there is a distinction between the two) automatically.
Depending on exactly what you want to do, you may want to create an array of series (e.g., a vector of series) and use the LABEL instruction to assign the appropriate labels to the individual series. This can be particularly useful if you already have series with the names in question on a data file and want to read them into a an array of series to simplify looping over the list of series.
If you will actually be creating the series in the program, you may want to create individual series with the constructed names instead. There are a few ways to do that, but the %S() function is often the easiest.
For example, this uses %S() to create new series on the fly, generating the name by concatenating a prefix label with an element of a vector of labels:
all 10
compute base = "tu12"
declare vector[labels] vl(2)
input vl
"Japan" "Germany"
do i=1,2
set %s(base+vl(i)) = i
end do i
print
A common application would be doing transformations of existing series. For example, suppose you already had GDP series called JPNGDP and GERJPD and wanted to generate logs of those called LJPNGDP and LGERJPD. You could do:
all 10
set gergdp = 10
set jpngdp = 100
dofor ser = gergdp jpngdp
log ser / %s("L"+%l(ser))
end dofor
print
Here, the %L() function returns the label of the current "ser" series, prepends that with an "L", and uses the result as the label for the new series created by the LOG instruction.
Hope this is helpful.
Regards,
Tom Maycock
Estima
Depending on exactly what you want to do, you may want to create an array of series (e.g., a vector of series) and use the LABEL instruction to assign the appropriate labels to the individual series. This can be particularly useful if you already have series with the names in question on a data file and want to read them into a an array of series to simplify looping over the list of series.
If you will actually be creating the series in the program, you may want to create individual series with the constructed names instead. There are a few ways to do that, but the %S() function is often the easiest.
For example, this uses %S() to create new series on the fly, generating the name by concatenating a prefix label with an element of a vector of labels:
all 10
compute base = "tu12"
declare vector[labels] vl(2)
input vl
"Japan" "Germany"
do i=1,2
set %s(base+vl(i)) = i
end do i
A common application would be doing transformations of existing series. For example, suppose you already had GDP series called JPNGDP and GERJPD and wanted to generate logs of those called LJPNGDP and LGERJPD. You could do:
all 10
set gergdp = 10
set jpngdp = 100
dofor ser = gergdp jpngdp
log ser / %s("L"+%l(ser))
end dofor
Here, the %L() function returns the label of the current "ser" series, prepends that with an "L", and uses the result as the label for the new series created by the LOG instruction.
Hope this is helpful.
Regards,
Tom Maycock
Estima