[Help] have a question about computing variables

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

[Help] have a question about computing variables

fateornot
Hi,

I am very new to the whole syntax and macro thing and I am trying to do some computations. I have done some searches but were not able to figure out by myself.

I have 744 variables, named consecutively. Let's say V1, V2, V3...V744. I want to add up every four variables (e.g., SUM(V1, V4), SUM(V5,V8), SUM(V9,V12), ...SUM(V741, V744). This will create 186 new variables, also to be named consecutively, let's say NewV1, NewV2, ..NewV186.

There is NO missing variables. How can I do this the easy way (compared to writing the same compute syntax for 186 times...)?

Thank you sooooooo much!

Lynn

Reply | Threaded
Open this post in threaded view
|

Re: [Help] have a question about computing variables

Maguin, Eugene
I assume the variables, v1 to v744, are contiguous, which makes it possible to exploit a vector structure.

Vector v=v1 to v744.
Numeric y1 to y186.
Vector y=y1 to y186.
Loop #i=1 to 186.
Compute #x1=v(#i-1)*4+1.
Compute #x2=v(#i-1)*4+2.
Compute #x3=v(#i-1)*4+3.
Compute #x4=v(#i-1)*4+4.
Compute y(#i)=sum(#x1 to #x4).
End loop.

This can be done more compactly.

Gene Maguin


-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of fateornot
Sent: Friday, September 06, 2013 8:23 PM
To: [hidden email]
Subject: [Help] have a question about computing variables

Hi,

I am very new to the whole syntax and macro thing and I am trying to do some computations. I have done some searches but were not able to figure out by myself.

I have 744 variables, named consecutively. Let's say V1, V2, V3...V744. I want to add up every four variables (e.g., SUM(V1, V4), SUM(V5,V8), SUM(V9,V12), ...SUM(V741, V744). This will create 186 new variables, also to be named consecutively, let's say NewV1, NewV2, ..NewV186.

There is NO missing variables. How can I do this the easy way (compared to writing the same compute syntax for 186 times...)?

Thank you sooooooo much!

Lynn





--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Help-have-a-question-about-computing-variables-tp5721859.html
Sent from the SPSSX Discussion mailing list archive at Nabble.com.

=====================
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: [Help] have a question about computing variables

Rich Ulrich
Martin, oops!
Those lines like 
   Compute #x1=v(#i-1)*4+1.
need an additional set of parentheses, like
   Compute #x1=v( (#i-1)*4+1 ).

I would probably write it all as


Vector v=v1 to v744.
Numeric y1 to y186.
Vector y=y1 to y186.
COMPUTE ## = 0.

Loop #=1 to 744 by 4.
COMPUTE ## = ##+1
Compute y(##)= v(#) + v(#+1) + v(#+2) + v(#+3).
End loop.

--
Rich Ulrich

> Date: Sat, 7 Sep 2013 00:36:31 +0000

> From: [hidden email]
> Subject: Re: [Help] have a question about computing variables
> To: [hidden email]
>
> I assume the variables, v1 to v744, are contiguous, which makes it possible to exploit a vector structure.
>
> Vector v=v1 to v744.
> Numeric y1 to y186.
> Vector y=y1 to y186.
> Loop #i=1 to 186.
> Compute #x1=v(#i-1)*4+1.
> Compute #x2=v(#i-1)*4+2.
> Compute #x3=v(#i-1)*4+3.
> Compute #x4=v(#i-1)*4+4.
> Compute y(#i)=sum(#x1 to #x4).
> End loop.
>
> This can be done more compactly.
>
> Gene Maguin
>
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of fateornot
> Sent: Friday, September 06, 2013 8:23 PM
> To: [hidden email]
> Subject: [Help] have a question about computing variables
>
> Hi,
>
> I am very new to the whole syntax and macro thing and I am trying to do some computations. I have done some searches but were not able to figure out by myself.
>
> I have 744 variables, named consecutively. Let's say V1, V2, V3...V744. I want to add up every four variables (e.g., SUM(V1, V4), SUM(V5,V8), SUM(V9,V12), ...SUM(V741, V744). This will create 186 new variables, also to be named consecutively, let's say NewV1, NewV2, ..NewV186.
>
> There is NO missing variables. How can I do this the easy way (compared to writing the same compute syntax for 186 times...)?
>
> Thank you sooooooo much!
>

Reply | Threaded
Open this post in threaded view
|

Re: [Help] have a question about computing variables

fateornot
In reply to this post by fateornot
Thank you so much for your quick reply!!! Your answers are incredibly helpful!

Unfortunately, I just realized I posted the wrong question. It turned out what I need is not to sum up every 4 variables. Here's a more detailed explanation of what I need to do.

I have some event history variables based on weeks. To be more specific, I have a list of variables that measures the number of hours worked by each person in each WEEK starting from the first week of 1997 thru the 25th week of 2012. These variables are continuous. Let's say these variables are named WEEK01, WEEK02....

I have another list of event history variables that tells me if the person was enrolled in college in each MONTH starting from the first month of 1997 thru the sixth month of 2012. These variables are binary (0, 1).Let's say these variables are named E01 E02...  

All variables are named consecutively.

I need to find out if a person has worked over 20 hours per WEEK on AVERAGE when they were enrolled in college. I do not need to know the exact total number of hours they worked in each week. So the new variable I need is a binary variable (0, 1) indicating if a person has worked over 20 hours (>20 hours) per week ON AVERAGE when they are ENROLLED in college.

Your help is highly appreciated!!!!!

PS. This is for a project I am working on for a Prof. If I don't get any replies in a week, I will have to use the stupid method and write thousands lines of compute command to get what I need.... That was what I did in the past two weeks...After writing thousands and thousands lines of commands, I discovered the easy way such as DO REPEAT. It was a life saver!!!

Thanks again!

Lynn
Reply | Threaded
Open this post in threaded view
|

Re: [Help] have a question about computing variables

Bruce Weaver
Administrator
I have a feeling your life might be easier if you restructured the data from WIDE to LONG (via VARSTOCASES).  Then you could use AGGREGATE with appropriate BREAK variables to get the sums or means you want.  And if so desired, you could go back to a wide file format via CASESTOVARS.

HTH.


fateornot wrote
Thank you so much for your quick reply!!! Your answers are incredibly helpful!

Unfortunately, I just realized I posted the wrong question. It turned out what I need is not to sum up every 4 variables. Here's a more detailed explanation of what I need to do.

I have some event history variables based on weeks. To be more specific, I have a list of variables that measures the number of hours worked by each person in each WEEK starting from the first week of 1997 thru the 25th week of 2012. These variables are continuous. Let's say these variables are named WEEK01, WEEK02....

I have another list of event history variables that tells me if the person was enrolled in college in each MONTH starting from the first month of 1997 thru the sixth month of 2012. These variables are binary (0, 1).Let's say these variables are named E01 E02...  

All variables are named consecutively.

I need to find out if a person has worked over 20 hours per WEEK on AVERAGE when they were enrolled in college. I do not need to know the exact total number of hours they worked in each week. So the new variable I need is a binary variable (0, 1) indicating if a person has worked over 20 hours (>20 hours) per week ON AVERAGE when they are ENROLLED in college.

Your help is highly appreciated!!!!!

PS. This is for a project I am working on for a Prof. If I don't get any replies in a week, I will have to use the stupid method and write thousands lines of compute command to get what I need.... That was what I did in the past two weeks...After writing thousands and thousands lines of commands, I discovered the easy way such as DO REPEAT. It was a life saver!!!

Thanks again!

Lynn
--
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: [Help] have a question about computing variables

Rich Ulrich
In reply to this post by fateornot
What a mess!

AGGREGATE and LOOP should be as valuable as DO REPEAT,
but before you start coding, you need to get some firm definitions.
I see a couple of serious ambiguities.

Weeks do not parse evenly into Months.  Do you need to
classify a Week that spans two months according to the
month where Monday falls, or Wednesday, or other?  Or is
this somehow made simple by the data?

"Working ... an average of 20 hours" needs careful specification.

What does a set of (21, 21, 21, 0)  for a month indicate?  The
average hours is less than 20, but there are 3 of 4 weeks with
more than 20.  If you omit the "zero", you have an average greater
than 20.  But if the zero were 5, then the average is less, with
more work done. 

The actual pattern of Hours is something that I expect should
interfere with the application of "simple logic", because the hours
are (I expect) going to have a very lumpy distribution:  Work-study
programs will have rules (amongst several sources of rules), and
those rules might have varied across the 15 years.  - I'm suggesting
that the "lumpiness" might not be the same across time, but the
rules you choose should be intelligible for every year.

 - I expect that, given the same data, I would preserve and work with
Hours (per month?) for as long as possible.  (a) Someone will ask about
that, eventually, assuming that these numbers are ever used for anything,
and (b) keeping those numbers in mind will help guide decisions in
defining the *meaningful* cutoff points.

--
Rich Ulrich

> Date: Sat, 7 Sep 2013 08:50:03 -0700

> From: [hidden email]
> Subject: Re: [Help] have a question about computing variables
> To: [hidden email]
>
> Thank you so much for your quick reply!!! Your answers are incredibly
> helpful!
>
> Unfortunately, I just realized I posted the wrong question. It turned out
> what I need is not to sum up every 4 variables. Here's a more detailed
> explanation of what I need to do.
>
> I have some event history variables based on weeks. To be more specific, I
> have a list of variables that measures the number of hours worked by each
> person in each WEEK starting from the first week of 1997 thru the 25th week
> of 2012. These variables are continuous. Let's say these variables are named
> WEEK01, WEEK02....
>
> I have another list of event history variables that tells me if the person
> was enrolled in college in each MONTH starting from the first month of 1997
> thru the sixth month of 2012. These variables are binary (0, 1).Let's say
> these variables are named E01 E02...
>
> All variables are named consecutively.
>
> I need to find out if a person has worked over 20 hours per WEEK on AVERAGE
> when they were enrolled in college. I do not need to know the exact total
> number of hours they worked in each week. So the new variable I need is a
> binary variable (0, 1) indicating if a person has worked over 20 hours (>20
> hours) per week ON AVERAGE when they are ENROLLED in college.
>
> Your help is highly appreciated!!!!!
>
> PS. This is for a project I am working on for a Prof. If I don't get any
> replies in a week, I will have to use the stupid method and write thousands
> lines of compute command to get what I need.... That was what I did in the
> past two weeks...After writing thousands and thousands lines of commands, I
> discovered the easy way such as DO REPEAT. It was a life saver!!!
>
> Thanks again!
>
> Lynn
>
>
>
> --
> View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Help-have-a-question-about-computing-variables-tp5721859p5721864.html
> Sent from the SPSSX Discussion mailing list archive at Nabble.com.
>
> =====================
> 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