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

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 refer to a specific date in a series within a loop?

Unread post 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.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post 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
macro
Posts: 70
Joined: Thu Jul 16, 2015 3:01 pm

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

Unread post by macro »

I'm not sure how I missed that. Thanks, that works perfectly.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

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

Unread post 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
Post Reply