DO IF statement loop

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

DO IF statement loop

Steve-41
Hey all,

I was looking through some syntax that is used to code for a variable
(1=yes, 2=No) based on whether a certain value appears in any one of 25
variables.  Presently the code looks like this:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compute newvar = 0.
if (bd1 >=200) & (bd1 <=299) newvar = 1.
if (bd2 >=200) & (bd2 <=299) newvar = 1.
if (bd3 >=200) & (bd3 <=299) newvar = 1.
.
.
.
if (bd24 >=200) & (bd24 <=299) newvar = 1.
if (bd25 >=200) & (bd25 <=299) newvar = 1.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Needless to say this is extremely cumbersome. Since I was modifying the
syntax file for some other analysis, I thought I might as well streamline
this part a bit too.  What I came up with was:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
COMPUTE newvar=0.
DO REPEAT a=bd1 TO bd25.
  DO IF (a>=200) & (a <=299).
    COMPUTE newvar=1.
  END IF.
END REPEAT.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


This little bit of code works fine.  I wanted to know if there is some
other way of doing the same thing - a simpler, easier, neater way than the
DO REPEAT statements?  I know that the ANY(varname, value1, value2,
value3) code looks for different values within the same variable, but is
there a way of looking at the same value (or a set of values) in different
variables?

Thanks.

- Steve

=====================
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:DO IF statement loop

Jerabek Jindrich
Hi Steve,

Your DO REPEAT syntax is just perfect.

You might simplify it a little using Range (a,200,299) instead of IF (a>=200) & (a <=299)., but it is a small not important improvement.

regards
Jindra

> ------------ Původní zpráva ------------
> Od: Steve <[hidden email]>
> Předmět: DO IF statement loop
> Datum: 07.7.2008 22:17:19
> ----------------------------------------
> Hey all,
>
> I was looking through some syntax that is used to code for a variable
> (1=yes, 2=No) based on whether a certain value appears in any one of 25
> variables.  Presently the code looks like this:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> compute newvar = 0.
> if (bd1 >=200) & (bd1 <=299) newvar = 1.
> if (bd2 >=200) & (bd2 <=299) newvar = 1.
> if (bd3 >=200) & (bd3 <=299) newvar = 1.
> .
> .
> .
> if (bd24 >=200) & (bd24 <=299) newvar = 1.
> if (bd25 >=200) & (bd25 <=299) newvar = 1.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> Needless to say this is extremely cumbersome. Since I was modifying the
> syntax file for some other analysis, I thought I might as well streamline
> this part a bit too.  What I came up with was:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> COMPUTE newvar=0.
> DO REPEAT a=bd1 TO bd25.
>   DO IF (a>=200) & (a <=299).
>     COMPUTE newvar=1.
>   END IF.
> END REPEAT.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> This little bit of code works fine.  I wanted to know if there is some
> other way of doing the same thing - a simpler, easier, neater way than the
> DO REPEAT statements?  I know that the ANY(varname, value1, value2,
> value3) code looks for different values within the same variable, but is
> there a way of looking at the same value (or a set of values) in different
> variables?
>
> Thanks.
>
> - Steve
>
> =====================
> 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: DO IF statement loop

ViAnn Beadle
Is the goal here to minimize the number of characters in the syntax file? If
so, here's an semi-obscure way to do it. This assumes as in your cases that
bd1 to bd25 are in file order.

Recode bd1 to b25 (200 thru 299=1)(else=0) into #newvar1 to #newvar25.
Compute var1 = any(1, newvar1 to newvar25).


---Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Jerabek Jindrich
Sent: Monday, July 07, 2008 4:09 PM
To: [hidden email]
Subject: Re:DO IF statement loop

Hi Steve,

Your DO REPEAT syntax is just perfect.

You might simplify it a little using Range (a,200,299) instead of IF
(a>=200) & (a <=299)., but it is a small not important improvement.

regards
Jindra

> ------------ Původní zpráva ------------
> Od: Steve <[hidden email]>
> Předmět: DO IF statement loop
> Datum: 07.7.2008 22:17:19
> ----------------------------------------
> Hey all,
>
> I was looking through some syntax that is used to code for a variable
> (1=yes, 2=No) based on whether a certain value appears in any one of 25
> variables.  Presently the code looks like this:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> compute newvar = 0.
> if (bd1 >=200) & (bd1 <=299) newvar = 1.
> if (bd2 >=200) & (bd2 <=299) newvar = 1.
> if (bd3 >=200) & (bd3 <=299) newvar = 1.
> .
> .
> .
> if (bd24 >=200) & (bd24 <=299) newvar = 1.
> if (bd25 >=200) & (bd25 <=299) newvar = 1.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> Needless to say this is extremely cumbersome. Since I was modifying the
> syntax file for some other analysis, I thought I might as well streamline
> this part a bit too.  What I came up with was:
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> COMPUTE newvar=0.
> DO REPEAT a=bd1 TO bd25.
>   DO IF (a>=200) & (a <=299).
>     COMPUTE newvar=1.
>   END IF.
> END REPEAT.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> This little bit of code works fine.  I wanted to know if there is some
> other way of doing the same thing - a simpler, easier, neater way than the
> DO REPEAT statements?  I know that the ANY(varname, value1, value2,
> value3) code looks for different values within the same variable, but is
> there a way of looking at the same value (or a set of values) in different
> variables?
>
> Thanks.
>
> - Steve
>
> =====================
> 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

=====================
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: Re:DO IF statement loop

Peck, Jon
In reply to this post by Jerabek Jindrich
ViAnn has posted a nice solution.  To which I would add as an aside,

 

ANY is actually more versatile than you suspect.

 

"ANY. ANY(test,value[,value,...]). Logical. Returns 1 or true if the value of test matches any of the subsequent values; returns 0 or false otherwise. This function requires two or more arguments. For example, ANY(var1, 1, 3, 5) returns 1 if the value of var1 is 1, 3, or 5 and 0 for other values. ANY can also be used to scan a list of variables or expressions for a value. For example, ANY(1, var1, var2, var3) returns 1 if any of the three specified variables has a value of 1 and 0 if all three variables have values other than 1. "

 

So ANY(1, a to z)

will be true if 1 is the value of any of the variables listed.

 

[snip]

>

>I know that the ANY(varname, value1, value2,

> value3) code looks for different values within the same variable, but is

> there a way of looking at the same value (or a set of values) in different

> variables?

>

> Thanks.

>

> - Steve

Reply | Threaded
Open this post in threaded view
|

Re: DO IF statement loop

ViAnn Beadle
I just noticed a typo in my solution. I forgot those pesky pound signs signifying temporary variables.

Compute var1 = any(1, #newvar1 to #newvar25).

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Peck, Jon
Sent: Monday, July 07, 2008 5:53 PM
To: [hidden email]
Subject: Re: Re:DO IF statement loop

ViAnn has posted a nice solution.  To which I would add as an aside,



ANY is actually more versatile than you suspect.



"ANY. ANY(test,value[,value,...]). Logical. Returns 1 or true if the value of test matches any of the subsequent values; returns 0 or false otherwise. This function requires two or more arguments. For example, ANY(var1, 1, 3, 5) returns 1 if the value of var1 is 1, 3, or 5 and 0 for other values. ANY can also be used to scan a list of variables or expressions for a value. For example, ANY(1, var1, var2, var3) returns 1 if any of the three specified variables has a value of 1 and 0 if all three variables have values other than 1. "



So ANY(1, a to z)

will be true if 1 is the value of any of the variables listed.



[snip]

>

>I know that the ANY(varname, value1, value2,

> value3) code looks for different values within the same variable, but is

> there a way of looking at the same value (or a set of values) in different

> variables?

>

> Thanks.

>

> - Steve

=====================
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: DO IF statement loop

Steve-41
In reply to this post by Steve-41
Thanks to ViAnn, Jindrich and Jon for their comments on that query.  My
main purpose was to make the syntax smaller as it was way too verbose the
way it was written initially.  Jon, thanks for that tip on 'ANY'.  I
assumed it would only look for multiple values in one variable, but seems
like the opposite is true as well.  That should definitely come in handy.
Thanks all.

- Steve


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jindrich wrote on Mon, Jul 7, 2008 at 5:09 PM:

You might simplify it a little using Range (a,200,299) instead of IF
(a>=200) & (a <=299)., but it is a small not important improvement.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ViAnn wrote on Mon Jul 7, 2008 at 5:38 PM:

Is the goal here to minimize the number of characters in the syntax file?
If so, here's an semi-obscure way to do it. This assumes as in your cases
that bd1 to bd25 are in file order.

Recode bd1 to b25 (200 thru 299=1)(else=0) into #newvar1 to #newvar25.
Compute var1 = any(1, #newvar1 to #newvar25).



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jon wrote on Mon Jul 7, 2008 at 6:53 PM:

ANY is actually more versatile than you suspect.

"ANY. ANY(test,value[,value,...]). Logical. Returns 1 or true if the value
of test matches any of the subsequent values; returns 0 or false
otherwise. This function requires two or more arguments. For example, ANY
(var1, 1, 3, 5) returns 1 if the value of var1 is 1, 3, or 5 and 0 for
other values. ANY can also be used to scan a list of variables or
expressions for a value. For example, ANY(1, var1, var2, var3) returns 1
if any of the three specified variables has a value of 1 and 0 if all
three variables have values other than 1. "

So ANY(1, a to z)
will be true if 1 is the value of any of the variables listed.

=====================
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: DO IF statement loop

ViAnn Beadle
Generally speaking verbosity is a very good thing, especially when tracking
down that pesky bug and if anyone else ever wants to re-engineer your code.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Steve
Sent: Wednesday, July 09, 2008 12:04 PM
To: [hidden email]
Subject: Re: DO IF statement loop

Thanks to ViAnn, Jindrich and Jon for their comments on that query.  My
main purpose was to make the syntax smaller as it was way too verbose the
way it was written initially.  Jon, thanks for that tip on 'ANY'.  I
assumed it would only look for multiple values in one variable, but seems
like the opposite is true as well.  That should definitely come in handy.
Thanks all.

- Steve


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jindrich wrote on Mon, Jul 7, 2008 at 5:09 PM:

You might simplify it a little using Range (a,200,299) instead of IF
(a>=200) & (a <=299)., but it is a small not important improvement.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ViAnn wrote on Mon Jul 7, 2008 at 5:38 PM:

Is the goal here to minimize the number of characters in the syntax file?
If so, here's an semi-obscure way to do it. This assumes as in your cases
that bd1 to bd25 are in file order.

Recode bd1 to b25 (200 thru 299=1)(else=0) into #newvar1 to #newvar25.
Compute var1 = any(1, #newvar1 to #newvar25).



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Jon wrote on Mon Jul 7, 2008 at 6:53 PM:

ANY is actually more versatile than you suspect.

"ANY. ANY(test,value[,value,...]). Logical. Returns 1 or true if the value
of test matches any of the subsequent values; returns 0 or false
otherwise. This function requires two or more arguments. For example, ANY
(var1, 1, 3, 5) returns 1 if the value of var1 is 1, 3, or 5 and 0 for
other values. ANY can also be used to scan a list of variables or
expressions for a value. For example, ANY(1, var1, var2, var3) returns 1
if any of the three specified variables has a value of 1 and 0 if all
three variables have values other than 1. "

So ANY(1, a to z)
will be true if 1 is the value of any of the variables listed.

=====================
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