DO IF is Dead . LONG LIVE ?

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

DO IF is Dead . LONG LIVE ?

David Marso
Administrator
Rather than this Eyesore:

DATA LIST FREE / A.
BEGIN DATA
1 2 3 4 5
END DATA.
DO IF (A EQ 1).
COMPUTE B=2.
COMPUTE C=3.
ELSE IF (A EQ 2).
COMPUTE B=4.
COMPUTE C=9.
ELSE IF (A EQ 3).
COMPUTE B=8.
COMPUTE C=27.
ELSE IF (A EQ 4).
COMPUTE B=16.
COMPUTE C=81.
ELSE.
COMPUTE B=0.
COMPUTE C=0.
END IF.
LIST.

Come to me my precious ;-)

DATA LIST FREE / A.
BEGIN DATA
1 2 3 4 5
END DATA.
DO REPEAT AVal= 1 2 3 4 / BResult=2 4 8 16 / CResult=3 9 27 81.
COMPUTE B=SUM(B,(A EQ AVal)*BResult).
COMPUTE C=SUM(C,(A EQ AVal)*CResult).
END REPEAT.
LIST.

Or this monstrosity ;-)

MATRIX.
GET A /FILE */VARIABLES A.
COMPUTE ABCVal= {1, 2,  3,  4;  2, 4,  8, 16;   3, 9, 27, 81}.            
COMPUTE BC=MAKE(NROW(A),NROW(ABCVal)-1,0).
LOOP #=1 TO NROW(A).
LOOP ##=1 TO NCOL(ABCVal).
COMPUTE BC(#,:)=BC(#,:)+(A(#) EQ ABCVal(1,##)) *
{ABCVal(2,##),ABCVal(3,##)}.
END LOOP.
END LOOP.
PRINT BC.
END MATRIX.




-----
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?"
--
Sent from: http://spssx-discussion.1045642.n5.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
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: DO IF is Dead . LONG LIVE ?

Art Kendall
Of course AUTO INDENT would tame either snippet of syntax to some degree.



-----
Art Kendall
Social Research Consultants
--
Sent from: http://spssx-discussion.1045642.n5.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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: DO IF is Dead . LONG LIVE ?

Rich Ulrich
In reply to this post by David Marso

Maybe I don't see the origin of this thread. 


But if you want B and C to be 2**AVAL and 3**AVAL   for AVAL in the range 1-4,

why not do a simple test on AVAL  and do the computations directly?


--

Rich Ulrich


From: SPSSX(r) Discussion <[hidden email]> on behalf of David Marso <[hidden email]>
Sent: Sunday, July 22, 2018 7:19:44 AM
To: [hidden email]
Subject: DO IF is Dead . LONG LIVE ?
 
Rather than this Eyesore:

DATA LIST FREE / A.
BEGIN DATA
1 2 3 4 5
END DATA.
DO IF (A EQ 1).
COMPUTE B=2.
COMPUTE C=3.
ELSE IF (A EQ 2).
COMPUTE B=4.
COMPUTE C=9.
ELSE IF (A EQ 3).
COMPUTE B=8.
COMPUTE C=27.
ELSE IF (A EQ 4).
COMPUTE B=16.
COMPUTE C=81.
ELSE.
COMPUTE B=0.
COMPUTE C=0.
END IF.
LIST.

Come to me my precious ;-)

DATA LIST FREE / A.
BEGIN DATA
1 2 3 4 5
END DATA.
DO REPEAT AVal= 1 2 3 4 / BResult=2 4 8 16 / CResult=3 9 27 81.
COMPUTE B=SUM(B,(A EQ AVal)*BResult).
COMPUTE C=SUM(C,(A EQ AVal)*CResult).
END REPEAT.
LIST.

Or this monstrosity ;-)

MATRIX.
GET A /FILE */VARIABLES A.
COMPUTE ABCVal= {1, 2,  3,  4;  2, 4,  8, 16;   3, 9, 27, 81}.            
COMPUTE BC=MAKE(NROW(A),NROW(ABCVal)-1,0).
LOOP #=1 TO NROW(A).
LOOP ##=1 TO NCOL(ABCVal).
COMPUTE BC(#,:)=BC(#,:)+(A(#) EQ ABCVal(1,##)) *
{ABCVal(2,##),ABCVal(3,##)}.
END LOOP.
END LOOP.
PRINT BC.
END MATRIX.




-----
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?"
--
Sent from: http://spssx-discussion.1045642.n5.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: DO IF is Dead . LONG LIVE ?

Andy W
In reply to this post by David Marso
Given this example I would probably just do:

DO REPEAT AVal= 1 2 3 4 / BResult=2 4 8 16 / CResult=3 9 27 81.
  DO IF A = AVal.
    COMPUTE B=BResult.
    COMPUTE C=CResult.
  END IF.
END REPEAT.

Two more lines with the DO IF-END IF, but I think it is easier to parse. It
differs on the behavior for the last case, which is missing with this code
instead of 0.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.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
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: DO IF is Dead . LONG LIVE ?

David Marso
Administrator
In reply to this post by Rich Ulrich
Bad example on my end.  Relationship between A, B and C should be arbitrary. The point of the exercise is a demonstration of how to escape the tedium of DO IF.

On Sun, Jul 22, 2018, 1:50 PM Rich Ulrich <[hidden email]> wrote:

Maybe I don't see the origin of this thread. 


But if you want B and C to be 2**AVAL and 3**AVAL   for AVAL in the range 1-4,

why not do a simple test on AVAL  and do the computations directly?


--

Rich Ulrich


From: SPSSX(r) Discussion <[hidden email]> on behalf of David Marso <[hidden email]>
Sent: Sunday, July 22, 2018 7:19:44 AM
To: [hidden email]
Subject: DO IF is Dead . LONG LIVE ?
 
Rather than this Eyesore:

DATA LIST FREE / A.
BEGIN DATA
1 2 3 4 5
END DATA.
DO IF (A EQ 1).
COMPUTE B=2.
COMPUTE C=3.
ELSE IF (A EQ 2).
COMPUTE B=4.
COMPUTE C=9.
ELSE IF (A EQ 3).
COMPUTE B=8.
COMPUTE C=27.
ELSE IF (A EQ 4).
COMPUTE B=16.
COMPUTE C=81.
ELSE.
COMPUTE B=0.
COMPUTE C=0.
END IF.
LIST.

Come to me my precious ;-)

DATA LIST FREE / A.
BEGIN DATA
1 2 3 4 5
END DATA.
DO REPEAT AVal= 1 2 3 4 / BResult=2 4 8 16 / CResult=3 9 27 81.
COMPUTE B=SUM(B,(A EQ AVal)*BResult).
COMPUTE C=SUM(C,(A EQ AVal)*CResult).
END REPEAT.
LIST.

Or this monstrosity ;-)

MATRIX.
GET A /FILE */VARIABLES A.
COMPUTE ABCVal= {1, 2,  3,  4;  2, 4,  8, 16;   3, 9, 27, 81}.            
COMPUTE BC=MAKE(NROW(A),NROW(ABCVal)-1,0).
LOOP #=1 TO NROW(A).
LOOP ##=1 TO NCOL(ABCVal).
COMPUTE BC(#,:)=BC(#,:)+(A(#) EQ ABCVal(1,##)) *
{ABCVal(2,##),ABCVal(3,##)}.
END LOOP.
END LOOP.
PRINT BC.
END MATRIX.




-----
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?"
--
Sent from: http://spssx-discussion.1045642.n5.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
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: DO IF is Dead . LONG LIVE ?

David Marso
Administrator
In reply to this post by Andy W
Probably more efficient computationally.


Andy W wrote

> Given this example I would probably just do:
>
> DO REPEAT AVal= 1 2 3 4 / BResult=2 4 8 16 / CResult=3 9 27 81.
>   DO IF A = AVal.
>     COMPUTE B=BResult.
>     COMPUTE C=CResult.
>   END IF.
> END REPEAT.
>
> Two more lines with the DO IF-END IF, but I think it is easier to parse.
> It
> differs on the behavior for the last case, which is missing with this code
> instead of 0.
>
>
>
> -----
> Andy W

> apwheele@

> http://andrewpwheeler.wordpress.com/
> --
> Sent from: http://spssx-discussion.1045642.n5.nabble.com/
>
> =====================
> To manage your subscription to SPSSX-L, send a message to

> LISTSERV@.UGA

>  (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?"
--
Sent from: http://spssx-discussion.1045642.n5.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
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?"