Lists of Lists, DO REPEAT, MACRO etc: Friday fun ;-)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Lists of Lists, DO REPEAT, MACRO etc: Friday fun ;-)

David Marso
Administrator
/*  Consider a situation where you have several lists of variables and within
each list you wish to
/*  determine whether a specific value is present in a particular list.  
/*  In other words you have a list of lists and a parallel list of values.
/*  Say these lists of variables have been defined as a set of macros.

DEFINE !A ()  a,b,c,d,e,f,g !ENDDEFINE.
DEFINE !B ()  h,i,j,k,l,m,n !ENDDEFINE.
DEFINE !C ()  o,p,q,r,s,t,u !ENDDEFINE.

/*  Now say you wish to query these lists for specific values.
/*  say does list A contain a Q?
/*  does list B contain an S?
/*  does list C contain a W?

DATA LIST FREE /a b c d e f g h i j k  l m n o p q r s t u (21A1).
BEGIN DATA
A V B S U S I   C V S E H J K   D W G U Y Z T  
U S H N M K O   E W A C D B A   Q B R J Y Z C
END DATA.


COMPUTE Q_in_A=ANY("Q",!A).
COMPUTE S_in_B=ANY("S",!B).
COMPUTE W_in_C=ANY("W",!C).

/*  Results in:

COMPUTE Q_in_A=ANY("Q",a,b,c,d,e,f,g ).  
COMPUTE S_in_B=ANY("S",h,i,j,k,l,m,n ).
COMPUTE W_in_C=ANY("W",o,p,q,r,s,t,u ).

/*  Fine and dandy.

/*  Now say you tried to put that into a DO REPEAT block?
/*  Well, that falls apart due to the lists becoming jammed together ;-(

DO REPEAT result = Q_in_A   S_in_B   W_in_C
         /target = "Q" "S" "W"
         /search =!A !B !C .
COMPUTE result=ANY(target,search).
END REPEAT PRINT .

Results in:

DO REPEAT result = Q_in_A   S_in_B   W_in_C
 /target = "Q" "S" "W"
 /search =a,b,c,d,e,f,g h,i,j,k,l,m,n o,p,q,r,s,t,u .      

Well, that won't do.

Cool as cool can be workaround!

DEFINE !ANY (!POS !CHAREND('/') /!POS !CHAREND('/') / !POS !CMDEND )
!IF ( !HEAD(!1) !NE !NULL)!THEN
COMPUTE !HEAD(!1)=ANY(!HEAD(!2),!CONCAT('!',!HEAD(!3) ) ) .
!ANY !TAIL(!1) / !TAIL(!2) / !TAIL(!3) ).
!IFEND
!ENDDEFINE.

!ANY Q_in_A   S_in_B   W_in_C  /  "Q" "S" "W"  / A B C .

Results in

COMPUTE Q_in_A=ANY("Q",a,b,c,d,e,f,g ).  
COMPUTE S_in_B=ANY("S",h,i,j,k,l,m,n ).
COMPUTE W_in_C=ANY("W",o,p,q,r,s,t,u ).
 
Basically we create the macro call on the fly by !CONCATenating ! to the
front of the macro root name.
I just recently discovered this beautiful thing.
Enjoy, have a great weekend.



-----
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?"