Page 1 of 1

How do I refer to a specific date in a series within a loop?

Posted: Tue Sep 22, 2015 2:55 pm
by macro
This code

Code: Select all

calendar(q) 1947 1

set a 2010:1 2010:4 = 2
set b 2010:1 2010:4 = 3

compute dt = 2010:2
dofor s = a b
    display s(dt)
end dofor s
throws an error:

Code: Select all

calendar(q) 1947 1

set a 2010:1 2010:4 = 2
set b 2010:1 2010:4 = 3

compute dt = 2010:2
dofor s = a b
(01.0062)     display s(<<<<
## SX17. Missing Operator or Adjacent Operands
>>>>dt)end dofor s
Is it possible to refer to a specific date within a series when the code is referring to that series in a loop index? I looked in the index to the User's Guide for terms related to "indexing" and "series" but didn't have any luck. I know I could do something like this:

Code: Select all

calendar(q) 1947 1

set a 2010:1 2010:4 = 2
set b 2010:1 2010:4 = 3

compute dt = 2010:2
dofor s = a b
    statistics s dt dt
    display %mean
end dofor s
but that seems like overkill.

Re: How do I refer to a specific date in a series within a l

Posted: Tue Sep 22, 2015 4:05 pm
by TomDoan
You have to "cast" the S to a SERIES:

Code: Select all

dofor s = a b
    display ([series]s)(dt)
end dofor s

Re: How do I refer to a specific date in a series within a l

Posted: Tue Sep 22, 2015 4:18 pm
by macro
I'm not sure how I missed that. Thanks, that works perfectly.

Re: How do I refer to a specific date in a series within a l

Posted: Tue Sep 22, 2015 5:41 pm
by TomDoan
The [SERIES] cast used to be much more important, as (many versions ago) you couldn't use i{lag} notation that has an implicit cast to SERIES in it (it doesn't make sense otherwise). So it's pretty much only needed now if you want to do exactly what you're trying to do here. The only example that I have that still does that is in SPGRAPH.RPF which normalizes all the series to be zero at 1996:1:

Code: Select all

dofor i = canusxsr frausxsr jpnusxsr gbrusxsr
   compute base=([series]i)(1996:1)
   set i = 100*(i{0}/base - 1.0)
end dofor i