Computation within CONCAT

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Computation within CONCAT

Eric Black
I have a series of variables, for example monthly data that is
V2011_1
V2011_2
...
V2011_12

I also have a variable START that indicates the start month, coded 1-12.

What I would like is a macro that gives me the sum of V2011_series starting
with the start month and then the next 3 months.

In other words
If start = 1 SUM = v2011_1 + v2011_2 + v2011_3.
If start = 2 SUM = v2011_2 + v2011_3 + v2011_4.
...
If start = 10 SUM = v2011_10 + v2011_11 + v2011_12.


For repetitive tasks, I typically use Define/!Enddefine and then !do/!doend.

In this case, this would be something like this

Define !Months1_3 ().
!Do !Count = 1 !to 10.
If start = !COUNT SUM = !CONCAT('v2011_',!COUNT) +
!CONCAT('v2011_'!COUNT+1)...

Here is my question:  Can I have something like this !COUNT+1 in the CONCAT?
It doesn't work the way I wrote it above but I was wondering if there is a
way to do that.

Thanks Matt

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

Bruce Weaver
Administrator
Does this do what you want?

* Read in some data for testing.
data list list / start V2011_1 to V2011_12 (13f5.0).
begin data
1 1 2 3 4 5 6 7 8 9 10 11 12
2 1 2 3 4 5 6 7 8 9 10 11 12
3 12 11 10 9 8 7 6 5 4 3 2 1
4 12 11 10 9 8 7 6 5 4 3 2 1
end data.

compute mysum = 0. /* SUM is a function, so use mysum as variable name.
vector v = V2011_1 to V2011_12.
loop #i = start to (start + 2).
- compute mysum = mysum + v(#i).
end loop.
execute.
formats mysum (f5.0).
list.


matti wrote
I have a series of variables, for example monthly data that is
V2011_1
V2011_2
...
V2011_12

I also have a variable START that indicates the start month, coded 1-12.

What I would like is a macro that gives me the sum of V2011_series starting
with the start month and then the next 3 months.

In other words
If start = 1 SUM = v2011_1 + v2011_2 + v2011_3.
If start = 2 SUM = v2011_2 + v2011_3 + v2011_4.
...
If start = 10 SUM = v2011_10 + v2011_11 + v2011_12.


For repetitive tasks, I typically use Define/!Enddefine and then !do/!doend.

In this case, this would be something like this

Define !Months1_3 ().
!Do !Count = 1 !to 10.
If start = !COUNT SUM = !CONCAT('v2011_',!COUNT) +
!CONCAT('v2011_'!COUNT+1)...

Here is my question:  Can I have something like this !COUNT+1 in the CONCAT?
It doesn't work the way I wrote it above but I was wondering if there is a
way to do that.

Thanks Matt

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

Eric Black
In reply to this post by Eric Black
Thanks Bruce

That does work.  The only issue I have is that I overlay a Do/DoEnd loop since
I have to repeat this several times (I am not too familiar with the loop
command).  This works fine, the only issue I have is that I get an error
message that Vector V is already defined.  Is there a way to "release" the
vector at the end of each do-loop?

Matt

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

Bruce Weaver
Administrator
Please post the syntax that is not working.  Thanks.


matti wrote
Thanks Bruce

That does work.  The only issue I have is that I overlay a Do/DoEnd loop since
I have to repeat this several times (I am not too familiar with the loop
command).  This works fine, the only issue I have is that I get an error
message that Vector V is already defined.  Is there a way to "release" the
vector at the end of each do-loop?

Matt

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

Eric Black
In reply to this post by Eric Black
This is what I use right now (I pull data from one variable only in this case
and don't sum it up),



Define !Section1 ()

!do !cnt = 0 !to 12.
compute !CONCAT('Section',!cnt) = 0.
vector v = Section201001 to Section201112.
loop #i = (start+!cnt-1) to (start+!cnt-1).
- compute  !CONCAT('Section',!cnt) = !CONCAT('Section',!cnt)+ v(#i).
end loop.
!doend.
!enddefine.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

Bruce Weaver
Administrator
I'm struggling to work out exactly what it is you are trying to do.  Nevertheless...

Your loop isn't going anywhere.  On the first pass, when !cnt = 0, you are looping from #i = start-1 to start-1.  On the last pass, when !cnt = 12, you'll be looping from #i = start+11 to start+11.  Is that what you want to do?  Is #i ending up outside the range of values that can be used as in index for your vector?  What are the possible values of START?

Earlier, you said you're getting a message telling you the vector is already defined.  Try moving your VECTOR line up, like this:

Define !Section1 ()

vector v = Section201001 to Section201112.
!do !cnt = 0 !to 12
compute !CONCAT('Section',!cnt) = 0.
loop #i = (start+!cnt-1) to (start+!cnt-1).
- compute  !CONCAT('Section',!cnt) = !CONCAT('Section',!cnt)+ v(#i).
end loop.
!doend

!enddefine.

But having said that, I'm still not sure what you're trying to do, and wonder if it can be done without a macro.

Also, note that the only lines in your macro that should end with a period are lines of regular syntax.  For macro commands, no period is necessary, and putting one there can louse things up.


matti wrote
This is what I use right now (I pull data from one variable only in this case
and don't sum it up),



Define !Section1 ()

!do !cnt = 0 !to 12.
compute !CONCAT('Section',!cnt) = 0.
vector v = Section201001 to Section201112.
loop #i = (start+!cnt-1) to (start+!cnt-1).
- compute  !CONCAT('Section',!cnt) = !CONCAT('Section',!cnt)+ v(#i).
end loop.
!doend.
!enddefine.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

David Marso
Administrator
In reply to this post by Eric Black
Either declare the VECTOR outside the !DO !DOEND or run a procedure within.
Since your macro lacks parameters it strikes me as overkill.  Just use a regular LOOP.
Not sure I see the issue since each case has a single "start date" or correct me if this is wrong with an illustrative example of what you actually need to achieve WRT the "repeat several times" aspect of the task.

matti wrote
Thanks Bruce

That does work.  The only issue I have is that I overlay a Do/DoEnd loop since
I have to repeat this several times (I am not too familiar with the loop
command).  This works fine, the only issue I have is that I get an error
message that Vector V is already defined.  Is there a way to "release" the
vector at the end of each do-loop?

Matt

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

Eric Black
In reply to this post by Eric Black
Thanks Bruce

What I need is some way of "normalizing" data.  I have customers who started a
service in January 2011 and others who started in July of 2011.  Instead of
having the data by calendar month I need it by service month.  So it would be
"Month 1 of service", "Month 2 of service" etc. which pulls data from the
calendar month variables.
For the one who started in Jan 2010, M1=Jan, M2=Feb;  For the July person, it
would be M1=Jul, M2=Aug.

Matt

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

Marks, Jim
I haven't been following the thread, but you can use RANK to create a
sequencing variable for each id.

Something like:

RANK date BY user_id /RANK INTO month_seq.


Jim Marks
Director, Market Research
x1616

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Matt Kretschmer
Sent: Tuesday, January 31, 2012 3:59 PM
To: [hidden email]
Subject: Re: Computation within CONCAT

Thanks Bruce

What I need is some way of "normalizing" data.  I have customers who
started a
service in January 2011 and others who started in July of 2011.  Instead
of
having the data by calendar month I need it by service month.  So it
would be
"Month 1 of service", "Month 2 of service" etc. which pulls data from
the
calendar month variables.
For the one who started in Jan 2010, M1=Jan, M2=Feb;  For the July
person, it
would be M1=Jul, M2=Aug.

Matt

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Computation within CONCAT

David Marso
Administrator
In reply to this post by Eric Black
You truly need to read up on VECTOR and LOOP.
You are just shooting yourself in the foot with your macro!
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"