SPSS loops and IFs

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

SPSS loops and IFs

Derek Quinlan
Hi All,
 
I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.
The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.
 
At first I thought...
 
COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.
 
and so on... out to day_2000
 
But at 2000 days... that is a lot of IF statements!
 
I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.
 
Please someone save me from writing 2000 IF statements!!!
 
Cheers
Derek
 
 
 
 


--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7

Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Mike
Wouldn't the following work:
 
Compute days_sum=sum(Day_1 to Day_2000).
 
Assuming the Day_1 to Day200 are contiguous variables. Given
that your variables are coded 0 and 1, the sum would reflect the
number of days the child was present.  The following should
also work:
 
Count days_#=Day_1 to Day_2000 (1).
 
Mike Palij
New York University
 
----- Original Message -----
Sent: Friday, February 12, 2010 2:51 PM
Subject: SPSS loops and IFs

Hi All,
 
I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.
The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.
 
At first I thought...
 
COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.
 
and so on... out to day_2000
 
But at 2000 days... that is a lot of IF statements!
 
I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.
 
Please someone save me from writing 2000 IF statements!!!
 
Cheers
Derek
 
 
 
 


--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7

Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Rick Oliver-3
In reply to this post by Derek Quinlan

You might want to look into the Count command, as in:

count duration=day_1 to day_2000 (1).


From: Derek Quinlan <[hidden email]>
To: [hidden email]
Date: 02/12/2010 01:53 PM
Subject: SPSS loops and IFs
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi All,
 
I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.
The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.
 
At first I thought...
 
COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.
 
and so on... out to day_2000
 
But at 2000 days... that is a lot of IF statements!
 
I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.
 
Please someone save me from writing 2000 IF statements!!!
 
Cheers
Derek
 
 
 
 


--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7


Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Derek Quinlan
Hi Rick,
 
I had done something very similar to that in order to calculate how many days the child actiually attended, but what I was hoping to do here was to calculate the numeber of days between the first and last visit (i.e. if the child was seen on day_1 and the last time they were seen was Day_1338, then the duration would be 1338 days).
 
Cheers
Derek
 
 


 
On Fri, Feb 12, 2010 at 3:06 PM, Rick Oliver <[hidden email]> wrote:

You might want to look into the Count command, as in:

count duration=day_1 to day_2000 (1).


From: Derek Quinlan <[hidden email]>
To: [hidden email]
Date: 02/12/2010 01:53 PM
Subject: SPSS loops and IFs
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi All,
 
I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.
The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.
 
At first I thought...
 
COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.
 
and so on... out to day_2000
 
But at 2000 days... that is a lot of IF statements!
 
I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.
 
Please someone save me from writing 2000 IF statements!!!
 
Cheers
Derek
 
 
 
 


--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7





--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7

Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Bruce Weaver
Administrator
In reply to this post by Derek Quinlan
Derek Quinlan wrote
Hi All,

I have 2000 variables, each representing a single day (day_1 TO day_2000),
each containing either a 0 or a 1.... child absent vs. child present,
respectively.
The children are always present on day_1 and I would like to be able to
calculate the number of days between day_1 and the last day they were
present.

At first I thought...

COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.

and so on... out to day_2000

But at 2000 days... that is a lot of IF statements!

I have very little knowledge of loops and have been reading up on them...
but I can't quite get my head wrapped around how to set this up.

Please someone save me from writing 2000 IF statements!!!

Cheers
Derek






--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON
N6A 3K7
You said, "I would like to be able to calculate the number of days between day_1 and the last day they were present", excluding day 1.  If all of the variables are coded 1=present and 0=absent, why not just do this?

compute dayspresent = sum(day_2 TO day_2000).
exe.

--
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: SPSS loops and IFs

Derek Quinlan
Hi Andres,
 
That is how I calculated the number of days the child was present... akin to what Rick had suggested.
 
However, I want to calculate the number of days total (whether seen or not) between day_1 and the last day they were present.
So the child could have attended day_1 and day_145 only, which I have already calculated as 2 days present, but what I am looking for is a way to calculate that there were 145 days between first and last day present.
 
Cheers
Derek

 

 
 
 


 
On Fri, Feb 12, 2010 at 3:17 PM, Bruce Weaver <[hidden email]> wrote:
Derek Quinlan wrote:
>
> Hi All,
>
> I have 2000 variables, each representing a single day (day_1 TO day_2000),
> each containing either a 0 or a 1.... child absent vs. child present,
> respectively.
> The children are always present on day_1 and I would like to be able to
> calculate the number of days between day_1 and the last day they were
> present.
>
> At first I thought...
>
> COMPUTE Duration = 0.
> IF(Day_2 = 1)Duration = 2.
> IF(Day_3 = 1)Duration = 3.
>
> and so on... out to day_2000
>
> But at 2000 days... that is a lot of IF statements!
>
> I have very little knowledge of loops and have been reading up on them...
> but I can't quite get my head wrapped around how to set this up.
>
> Please someone save me from writing 2000 IF statements!!!
>

> Cheers
> Derek
>
>
>
>
>
>
> --
> Dr. Derek J. Quinlan
> Research Project Coordinator
> Psychology Lecturer
> University of Western Ontario
> London, Ontario

>
>
> Mailing Address:
> Dr. Derek J. Quinlan
> The University of Western Ontario
> Westminster Hall, Suite 326E
> London, ON
> N6A 3K7
>
>

You said, "I would like to be able to calculate the number of days between
day_1 and the last day they were present", excluding day 1.  If all of the
variables are coded 1=present and 0=absent, why not just do this?

compute dayspresent = sum(day_2 TO day_2000).
exe.



-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/
"When all else fails, RTFM."

NOTE:  My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.
--
View this message in context: http://old.nabble.com/SPSS-loops-and-IFs-tp27568533p27568814.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



--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7

Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Mike
If I understand you, you want to know what is the last day that a
child was present.  Working from Day_2000 backwards, consider
the following

subtitle 'Computing Last Day Student Attended'.

compute last_day= 2000.
vector Days_vec=Day_1 to Day_2000.
loop #i=1 to 2000 if ((days_vec(2000-#i) eq 1)).
compute last_day=(last_day - 1).
end loop.

var label last_day "Last Day Child Attended".
desc var=last_day.

-Mike Palij
New York University
[hidden email]



----- Original Message -----
From: Derek Quinlan
To: [hidden email]
Sent: Friday, February 12, 2010 3:24 PM
Subject: Re: SPSS loops and IFs


Hi Andres,

That is how I calculated the number of days the child was present... akin to what Rick had suggested.

However, I want to calculate the number of days total (whether seen or not) between day_1 and the last day they were present.
So the child could have attended day_1 and day_145 only, which I have already calculated as 2 days present, but what I am looking for is a way to calculate that there were 145 days between first and last day present.

Cheers
Derek







On Fri, Feb 12, 2010 at 3:17 PM, Bruce Weaver <[hidden email]> wrote:

Derek Quinlan wrote:

>
> Hi All,
>
> I have 2000 variables, each representing a single day (day_1 TO day_2000),
> each containing either a 0 or a 1.... child absent vs. child present,
> respectively.
> The children are always present on day_1 and I would like to be able to
> calculate the number of days between day_1 and the last day they were
> present.
>
> At first I thought...
>
> COMPUTE Duration = 0.
> IF(Day_2 = 1)Duration = 2.
> IF(Day_3 = 1)Duration = 3.
>
> and so on... out to day_2000
>
> But at 2000 days... that is a lot of IF statements!
>
> I have very little knowledge of loops and have been reading up on them...
> but I can't quite get my head wrapped around how to set this up.
>
> Please someone save me from writing 2000 IF statements!!!
>
> Cheers
> Derek
>
>
>
>
>
>
> --
> Dr. Derek J. Quinlan
> Research Project Coordinator
> Psychology Lecturer
> University of Western Ontario
> London, Ontario
>
>
> Mailing Address:
> Dr. Derek J. Quinlan
> The University of Western Ontario
> Westminster Hall, Suite 326E
> London, ON
> N6A 3K7
>
>


You said, "I would like to be able to calculate the number of days between
day_1 and the last day they were present", excluding day 1.  If all of the
variables are coded 1=present and 0=absent, why not just do this?

compute dayspresent = sum(day_2 TO day_2000).
exe.



-----
--
Bruce Weaver
[hidden email]
http://sites.google.com/a/lakeheadu.ca/bweaver/
"When all else fails, RTFM."

NOTE:  My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.
--
View this message in context: http://old.nabble.com/SPSS-loops-and-IFs-tp27568533p27568814.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




--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON
N6A 3K7

=====================
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: SPSS loops and IFs

Art Kendall
In reply to this post by Derek Quinlan
I don't have the time to do a test run but see if something like this
does it.
compute duration = 1.
do repeat day = day_2 to day2000/index=2 to 2000.
if day duration=index.
end repeat.

Art Kendall
Social Research Consultant


On 2/12/2010 2:51 PM, Derek Quinlan wrote:

> Hi All,
> I have 2000 variables, each representing a single day (day_1 TO
> day_2000), each containing either a 0 or a 1.... child absent vs.
> child present, respectively.
> The children are always present on day_1 and I would like to be able
> to calculate the number of days between day_1 and the last day they
> were present.
> At first I thought...
> COMPUTE Duration = 0.
> IF(Day_2 = 1)Duration = 2.
> IF(Day_3 = 1)Duration = 3.
> and so on... out to day_2000
> But at 2000 days... that is a lot of IF statements!
> I have very little knowledge of loops and have been reading up on
> them... but I can't quite get my head wrapped around how to set this up.
> Please someone save me from writing 2000 IF statements!!!
> Cheers
> Derek
>
>
> --
> Dr. Derek J. Quinlan
> Research Project Coordinator
> Psychology Lecturer
> University of Western Ontario
> London, Ontario
>
>
> Mailing Address:
> Dr. Derek J. Quinlan
> The University of Western Ontario
> Westminster Hall, Suite 326E
> London, ON
> N6A 3K7
>

=====================
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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Richard Ristow
In reply to this post by Derek Quinlan
At 03:24 PM 2/12/2010, Derek Quinlan wrote:

I want to calculate the number of days total (whether seen or not) between day_1 and the last day they were present. So the child could have attended day_1 and day_145 only, which I have already calculated as 2 days present, but what I am looking for is a way to calculate that there were 145 days between first and last day present.

Untested -- some errors likely:

NUMERIC FirstDay LastDay (F5).
VECTOR  Day=day_1 TO day_2000.
LOOP  #Day=1 TO 2000.
.  DO IF Day(#Day) EQ 1.
.     COMPUTE FirstDay = MIN(#Day,FirstDay).
.     COMPUTE LastDay  = Max(#Day,LastDay).
.  END IF.
END LOOP.

COMPUTE Interval=LastDay-FirstDay+1.

(The "+1" is so both the first and last days are counted as part of the interval.)
===================== 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: SPSS loops and IFs

John F Hall
In reply to this post by Derek Quinlan
How about: (unchecked and untested) something like?
 
do repeat
    x= 1 to 2000
    /y = day_1 to Day_2000
if y = 1 days = x.
end repeat.
----- Original Message -----
Sent: Friday, February 12, 2010 9:14 PM
Subject: Re: SPSS loops and IFs

Hi Rick,
 
I had done something very similar to that in order to calculate how many days the child actiually attended, but what I was hoping to do here was to calculate the numeber of days between the first and last visit (i.e. if the child was seen on day_1 and the last time they were seen was Day_1338, then the duration would be 1338 days).
 
Cheers
Derek
 
 


 
On Fri, Feb 12, 2010 at 3:06 PM, Rick Oliver <[hidden email]> wrote:

You might want to look into the Count command, as in:

count duration=day_1 to day_2000 (1).


From: Derek Quinlan <[hidden email]>
To: [hidden email]
Date: 02/12/2010 01:53 PM
Subject: SPSS loops and IFs
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi All,
 
I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.
The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.
 
At first I thought...
 
COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.
 
and so on... out to day_2000
 
But at 2000 days... that is a lot of IF statements!
 
I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.
 
Please someone save me from writing 2000 IF statements!!!
 
Cheers
Derek
 
 
 
 


--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7





--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7

Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Derek Quinlan
In reply to this post by Derek Quinlan
Thanks to everyone that offered their suggestions/help with my original syntax problem (as seen in the original email post way below).
 
Here's what worked.
 
vector vec=day_1 to day_2000.
loop #i=2000 to 2 by -1.
do if vec(#i)=1.
compute duration=#i.
break.
end if.
end loop.
EXECUTE.
 
 
NEW PROBLEM:
I'd like to figure out how many days the child attended during the 30 days previous to their last visit.
(e.g. Child was last seen on day_1800... then count number of attended days from 1800 back to 1770)
 
Can't figure out how to work the 30 day part into this.
 
Any help would be greatly appreciated.
 
Derek
 
 


 
 
 
 
 
 
 
 
On Fri, Feb 12, 2010 at 3:51 PM, Derek Quinlan <[hidden email]> wrote:
Hi All,
 
I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.
The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.
 
At first I thought...
 
COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.
 
and so on... out to day_2000
 
But at 2000 days... that is a lot of IF statements!
 
I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.
 
Please someone save me from writing 2000 IF statements!!!
 
Cheers
Derek
 
 
 
 


--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7




--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7

Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Albert-Jan Roskam
Hi,
 
This is slightly shorter:
 
vector day = day_1 to day_2000.
loop #i = 2000 to 2 by -1.
+compute duration = #i.
end loop if (day(#i) eq 1).
I didn't look into your original email with a lot of attention, but here's a wild guess about your new query:
 
compute #counter = 0.
loop #i = 1 to duration by -1.
+compute #counter = #counter + 1.
+compute outcome = #i.
end loop if(#counter eq 30).
 
Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Wed, 6/9/10, Derek Quinlan <[hidden email]> wrote:

From: Derek Quinlan <[hidden email]>
Subject: Re: [SPSSX-L] SPSS loops and IFs
To: [hidden email]
Date: Wednesday, June 9, 2010, 9:34 PM

Thanks to everyone that offered their suggestions/help with my original syntax problem (as seen in the original email post way below).
 
Here's what worked.
 
vector vec=day_1 to day_2000.
loop #i=2000 to 2 by -1.
do if vec(#i)=1.
compute duration=#i.
break.
end if.
end loop.
EXECUTE.
 
 
NEW PROBLEM:
I'd like to figure out how many days the child attended during the 30 days previous to their last visit.
(e.g. Child was last seen on day_1800... then count number of attended days from 1800 back to 1770)
 
Can't figure out how to work the 30 day part into this.
 
Any help would be greatly appreciated.
 
Derek
 
 


 
 
 
 
 
 
 
 
On Fri, Feb 12, 2010 at 3:51 PM, Derek Quinlan <derek.james.quinlan@...> wrote:
Hi All,
 
I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.
The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.
 
At first I thought...
 
COMPUTE Duration = 0.
IF(Day_2 = 1)Duration = 2.
IF(Day_3 = 1)Duration = 3.
 
and so on... out to day_2000
 
But at 2000 days... that is a lot of IF statements!
 
I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.
 
Please someone save me from writing 2000 IF statements!!!
 
Cheers
Derek
 
 
 
 


--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7




--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7


Reply | Threaded
Open this post in threaded view
|

Re: SPSS loops and IFs

Maguin, Eugene
In reply to this post by Derek Quinlan
Derek,

I must be not understanding something about both your original question and
this one but as I understand your original question you might have data like
this for a kid.
Id d1 .... D20
12 10011111001001111100

You said: The children are always present on day_1 and I would like to be
able to calculate the number of days between day_1 and the last day they
were present.

It seems to me that this would work for your original question.

Count dayspresent=d2 to d20(1).
If (dayspresent ne 0) dayspresent=dayspresent-1.

Rationale. There's two cases to consider. 1) never present after day 1. the
count command returns a value of 0 but you don't need to decrement that by
the last day because there was none. 2) present one or more times after day
1. the count returns the number of days including the last day, which you
don't want to count. Therefore, subtract 1 from the count to exclude the
last day.

The new question is different because you need to find that last day present
and look backwards 30 days and count the number of days present in that 30
period beginning, I'm assuming, with the day previous to the day of the last
visit. For this you have to use a loop structure because you have to find
that last visit. So this would be my whole syntax package.


Count dayspresent=d2 to d20(1).
If (dayspresent ne 0) dayspresent=dayspresent-1.
Compute past30days=0.
Compute #last=0.
vector day = day_1 to day_2000.
loop #i = 2000 to 2 by -1.
+  if (day(#i) eq 1 and #last eq 0) #last=#i.
+  if ((#last-#i) ge 1) past30days=past30days+days(#i).
end loop if ((#last-#i) eq 30).

Gene Maguin



Thanks to everyone that offered their suggestions/help with my original
syntax problem (as seen in the original email post way below).

Here's what worked.

vector vec=day_1 to day_2000.
loop #i=2000 to 2 by -1.
do if vec(#i)=1.
compute duration=#i.
break.
end if.
end loop.
EXECUTE.


NEW PROBLEM:
I'd like to figure out how many days the child attended during the 30 days
previous to their last visit.
(e.g. Child was last seen on day_1800... then count number of attended days
from 1800 back to 1770)

Can't figure out how to work the 30 day part into this.

Any help would be greatly appreciated.

Derek

>>>On Fri, Feb 12, 2010 at 3:51 PM, Derek Quinlan
<[hidden email]> wrote:

        I have 2000 variables, each representing a single day (day_1 TO
day_2000), each containing either a 0 or a 1.... child absent vs. child
present, respectively.
        The children are always present on day_1 and I would like to be able
to calculate the number of days between day_1 and the last day they were
present.

        At first I thought...

        COMPUTE Duration = 0.
        IF(Day_2 = 1)Duration = 2.
        IF(Day_3 = 1)Duration = 3.

        and so on... out to day_2000

        But at 2000 days... that is a lot of IF statements!

        I have very little knowledge of loops and have been reading up on
them... but I can't quite get my head wrapped around how to set this up.

        Please someone save me from writing 2000 IF statements!!!

        Cheers
        Derek

=====================
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: SPSS loops and IFs

Langston, Eric
In reply to this post by Derek Quinlan

Derek-

 

Do you want to count their last visit in the 30 days?  In your example, counting 1800 back to 1770 is actually 31 days.  So, do you want to count 1770 to 1799 or 1771 to 1800?  Also, what if the last day of attendance was within the first 30 days?  I’ll assume you’d just take their total attendance.

 

Since you said that day_1 is always 1, then your duration variable is also equal to the last day of attendance.  So, I think the best solution is to use the same vector, but just not with a loop this time.  For example, “vec(duration)” would be the dichotomous day variable of their last day of attendance and “vec(duration-1)” would be the day before their last day and so on.  I tried to use the TO keyword in the sum but it gave me an error – perhaps one of the other list members can help me understand why?  [ compute last30=sum( vec(duration-30) TO vec(duration) ).  /* Gives an error. ]  But since you only have 30 days, it’s not awful to just have all 30 listed out.

 

The tested syntax is below.  If you want to count the last day in the “last30”, then delete “vec(duration-30)”.  If you don’t want to count the last day, then delete “vec(duration)”.  As it is currently written, it’s actually counting 31 days.  Similarly, you may need to adjust the integer in the IF statement – “if duration<=31” and “do if duration>31”.  Did I explain that well?

 

Hope that helps,

 

-Eric

 

 

 

vector vec=day_1 to day_2000.

* Find the duration of attendance.

loop #i=2000 to 2 by -1.

do if vec(#i)=1.

compute duration=#i.

break.

end if.

end loop.

* Find the attendance for 30 days prior to last day.

if duration<30 last30=sum(day_1 to day_30).

do if duration>30.

compute last30=sum( vec(duration),

vec(duration-1),

vec(duration-2),

vec(duration-3),

vec(duration-4),

vec(duration-5),

vec(duration-6),

vec(duration-7),

vec(duration-8),

vec(duration-9),

vec(duration-10),

vec(duration-11),

vec(duration-12),

vec(duration-13),

vec(duration-14),

vec(duration-15),

vec(duration-16),

vec(duration-17),

vec(duration-18),

vec(duration-19),

vec(duration-20),

vec(duration-21),

vec(duration-22),

vec(duration-23),

vec(duration-24),

vec(duration-25),

vec(duration-26),

vec(duration-27),

vec(duration-28),

vec(duration-29),

vec(duration-30)).

end if.

exe.

 

 

 

From: Derek Quinlan [mailto:[hidden email]]
Sent: Wednesday, June 09, 2010 2:35 PM
Subject: Re: SPSS loops and IFs

 

Thanks to everyone that offered their suggestions/help with my original syntax problem (as seen in the original email post way below).

 

Here's what worked.

 

vector vec=day_1 to day_2000.
loop #i=2000 to 2 by -1.
do if vec(#i)=1.
compute duration=#i.
break.
end if.
end loop.
EXECUTE.

 

 

NEW PROBLEM:

I'd like to figure out how many days the child attended during the 30 days previous to their last visit.

(e.g. Child was last seen on day_1800... then count number of attended days from 1800 back to 1770)

 

Can't figure out how to work the 30 day part into this.

 

Any help would be greatly appreciated.

 

Derek

 

 



 

 

 

 

 

 

 

 

On Fri, Feb 12, 2010 at 3:51 PM, Derek Quinlan <[hidden email]> wrote:

Hi All,

 

I have 2000 variables, each representing a single day (day_1 TO day_2000), each containing either a 0 or a 1.... child absent vs. child present, respectively.

The children are always present on day_1 and I would like to be able to calculate the number of days between day_1 and the last day they were present.

 

At first I thought...

 

COMPUTE Duration = 0.

IF(Day_2 = 1)Duration = 2.

IF(Day_3 = 1)Duration = 3.

 

and so on... out to day_2000

 

But at 2000 days... that is a lot of IF statements!

 

I have very little knowledge of loops and have been reading up on them... but I can't quite get my head wrapped around how to set this up.

 

Please someone save me from writing 2000 IF statements!!!

 

Cheers

Derek

 

 

 

 



--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7




--
Dr. Derek J. Quinlan
Research Project Coordinator
Psychology Lecturer
University of Western Ontario
London, Ontario


Mailing Address:
Dr. Derek J. Quinlan
The University of Western Ontario
Westminster Hall, Suite 326E
London, ON  
N6A 3K7