using loop to compare text between two variables

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

using loop to compare text between two variables

Bren Tagget
Hello all,

I have a long list of records with text names in two columns, x1 and x2. I want to compare x2 names with x1 names and identify those that match any instance of x1 and note it in temp variable. I have included sample data with what I want to have happen. I've tried looping through the two columns (see syntax below) but I get a "y" at every record. The 77700 is the number of cases I need to have the syntax loop through.
I hope someone can show me the correct syntax.
Thanks
Bren
 
x1
x2
temp
Greg Smith
Allen Anderson
n
Greg Smith
Jack Lovin
y
Tom Watson
Henry Jones
n
Ellen Hampton
Jack Lovin
y
Jack Lovin
Fred Dover
y
Jack Lovin
Alice Wanders
n
Jack Lovin
Patrick Evans
n
Jack Lovin
Thomas Smith
n
Fred Dover
Kelly Bunson
n
 
string temp(a1).
compute temp= "n".
vector x=x1 to x2.
loop #i=1 TO 77700.
loop #j = (#i+1) to 77700.
if x(#i)=x(#j) temp = "y".
end loop.
end loop.
execute.
 

===================== 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: using loop to compare text between two variables

David Marso
Administrator
NOTE:  LOOP is a TRANSFORMATION and applies to single cases (ie it cannot cross rows).
You could do this in MATRIX (but that my friend would be a waste when you have AGGREGATE and MATCH FILES at your disposal).
Consider:

DATA LIST / v1 v2 (2a1) .
BEGIN DATA
ab
ac
aq
ba
cz
df
er
zt
END DATA.
DATASET NAME raw.
COMPUTE raw_order=$CASENUM.
SORT CASES BY v2.
STRING key (a1).
COMPUTE key=v2.
AGGREGATE OUTFILE * / BREAK v1 / found=N.
RENAME VARIABLES v1=key.
MATCH FILES FILE raw / TABLE * /IN=match / BY key .
SORT CASES BY raw_order.
LIST.

 
v1 v2 raw_order key   found match
 
a  b       1.00 b         1   1
a  c       2.00 c         1   1
a  q       3.00 q         .   0
b  a       4.00 a         3   1
c  z       5.00 z         1   1
d  f       6.00 f         .   0
e  r       7.00 r         .   0
z  t       8.00 t         .   0
 
 
Number of cases read:  8    Number of cases listed:  8

Bren Tagget wrote
Hello all,

I have a long list of
records with text names in two columns, x1 and x2. I want to compare x2 names
with x1 names and identify those that match any instance of x1 and note it in
temp variable. I have included sample data with what I want to have happen.
I've tried looping through the two columns (see syntax below) but I get a
"y" at every record. The 77700 is the number of cases I need to have the syntax loop through.
I hope someone can show
me the correct syntax.
Thanks
Bren
 
x1 x2 temp
Greg Smith Allen Anderson n
Greg Smith Jack Lovin y
Tom Watson Henry Jones n
Ellen Hampton Jack Lovin y
Jack Lovin Fred Dover y
Jack Lovin Alice Wanders n
Jack Lovin Patrick Evans n
Jack Lovin Thomas Smith n
Fred Dover Kelly Bunson n
 
string temp(a1).
compute temp= "n".
vector x=x1 to x2.
loop #i=1 TO 77700.
loop #j = (#i+1) to 77700.
if x(#i)=x(#j) temp = "y".
end loop.
end loop.
execute.

=====================
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: using loop to compare text between two variables

David Marso
Administrator
Even leaner:
DATA LIST / v1 v2 (2a1) .
BEGIN DATA
ab
ac
aq
ba
cz
df
er
zt
END DATA.

DATASET NAME raw.
COMPUTE raw_order=$CASENUM.
SORT CASES BY v2.
AGGREGATE OUTFILE * / BREAK v1 / v2=MAX(v1).
MATCH FILES FILE raw / TABLE * /IN=match / BY v2 .
SORT CASES BY raw_order.
DELETE VARIABLES raw_order.
LIST.
 
 
v1 v2 match
 
a  b    1
a  c    1
a  q    0
b  a    1
c  z    1
d  f    0
e  r    0
z  t    0
 
 
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: using loop to compare text between two variables

Bruce Weaver
Administrator
Very nice, David.  I was thinking along the same lines, but had not yet arrived at anything that efficient.  

David Marso wrote
Even leaner:
DATA LIST / v1 v2 (2a1) .
BEGIN DATA
ab
ac
aq
ba
cz
df
er
zt
END DATA.

DATASET NAME raw.
COMPUTE raw_order=$CASENUM.
SORT CASES BY v2.
AGGREGATE OUTFILE * / BREAK v1 / v2=MAX(v1).
MATCH FILES FILE raw / TABLE * /IN=match / BY v2 .
SORT CASES BY raw_order.
DELETE VARIABLES raw_order.
LIST.
 
 
v1 v2 match
 
a  b    1
a  c    1
a  q    0
b  a    1
c  z    1
d  f    0
e  r    0
z  t    0
--
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: using loop to compare text between two variables

Jon K Peck
In reply to this post by David Marso
Even leaner still.

dataset copy names2.
spssinc trans result = match type=20
/initial  "extendedTransforms.vlookup('x1', 'x1', 'names2')"
/formula func(x2).


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        11/12/2014 11:45 AM
Subject:        Re: [SPSSX-L] using loop to compare text between two variables
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Even leaner:
DATA LIST / v1 v2 (2a1) .
BEGIN DATA
ab
ac
aq
ba
cz
df
er
zt
END DATA.

DATASET NAME raw.
COMPUTE raw_order=$CASENUM.
SORT CASES BY v2.
AGGREGATE OUTFILE * / BREAK v1 / v2=MAX(v1).
MATCH FILES FILE raw / TABLE * /IN=match / BY v2 .
SORT CASES BY raw_order.
DELETE VARIABLES raw_order.
LIST.


v1 v2 match

a  b    1
a  c    1
a  q    0
b  a    1
c  z    1
d  f    0
e  r    0
z  t    0





-----
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?"
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/using-loop-to-compare-text-between-two-variables-tp5727892p5727897.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: using loop to compare text between two variables

Bren Tagget
In reply to this post by David Marso
David,

Both of these worked. Jon I haven't tested your code yet, but David's code worked beautifully. I really appreciate the help.

Bren


On Wednesday, November 12, 2014 12:44 PM, David Marso <[hidden email]> wrote:


Even leaner:
DATA LIST / v1 v2 (2a1) .
BEGIN DATA
ab
ac
aq
ba
cz
df
er
zt
END DATA.

DATASET NAME raw.
COMPUTE raw_order=$CASENUM.
SORT CASES BY v2.
AGGREGATE OUTFILE * / BREAK v1 / v2=MAX(v1).
MATCH FILES FILE raw / TABLE * /IN=match / BY v2 .
SORT CASES BY raw_order.
DELETE VARIABLES raw_order.
LIST.


v1 v2 match

a  b    1
a  c    1
a  q    0
b  a    1
c  z    1
d  f    0
e  r    0
z  t    0





-----
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?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/using-loop-to-compare-text-between-two-variables-tp5727892p5727897.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: using loop to compare text between two variables

Bruce Weaver
Administrator
Jon didn't say so, but I believe his solution requires Python.  David's is old-school native SPSS syntax--NPR (i.e., No Python Required)!




Bren Tagget wrote
David,

Both of these worked. Jon I haven't tested your code yet, but David's code worked beautifully. I really appreciate the help.

Bren


On Wednesday, November 12, 2014 12:44 PM, David Marso <[hidden email]> wrote:
 


Even leaner:
DATA LIST / v1 v2 (2a1) .
BEGIN DATA
ab
ac
aq
ba
cz
df
er
zt
END DATA.

DATASET NAME raw.
COMPUTE raw_order=$CASENUM.
SORT CASES BY v2.
AGGREGATE OUTFILE * / BREAK v1 / v2=MAX(v1).
MATCH FILES FILE raw / TABLE * /IN=match / BY v2 .
SORT CASES BY raw_order.
DELETE VARIABLES raw_order.
LIST.


v1 v2 match

a  b    1
a  c    1
a  q    0
b  a    1
c  z    1
d  f    0
e  r    0
z  t    0





-----
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?"
--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/using-loop-to-compare-text-between-two-variables-tp5727892p5727897.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
--
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: using loop to compare text between two variables

Jon K Peck
Yes, definitely requires the Python Essentials, which are installed standardly with Statistics since V22, and the extendedTransforms.py module from the SPSS Community site (www.ibm.com/developerworks/spssdevcentral).

The extendedTransforms.py module, BTW, includes a number of functions that can be used for fuzzy sorts of name comparisons that often come in handy when dealing with variations in names, but these are a little more complex to use.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Bruce Weaver <[hidden email]>
To:        [hidden email]
Date:        11/12/2014 03:21 PM
Subject:        Re: [SPSSX-L] using loop to compare text between two variables
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Jon didn't say so, but I /believe/ his solution requires Python.  David's is
old-school native SPSS syntax--NPR (i.e., No Python Required)!





Bren Tagget wrote
> David,
>
> Both of these worked. Jon I haven't tested your code yet, but David's code
> worked beautifully. I really appreciate the help.
>
> Bren
>
>
> On Wednesday, November 12, 2014 12:44 PM, David Marso &lt;

> david.marso@

> &gt; wrote:
>  
>
>
> Even leaner:
> DATA LIST / v1 v2 (2a1) .
> BEGIN DATA
> ab
> ac
> aq
> ba
> cz
> df
> er
> zt
> END DATA.
>
> DATASET NAME raw.
> COMPUTE raw_order=$CASENUM.
> SORT CASES BY v2.
> AGGREGATE OUTFILE * / BREAK v1 / v2=MAX(v1).
> MATCH FILES FILE raw / TABLE * /IN=match / BY v2 .
> SORT CASES BY raw_order.
> DELETE VARIABLES raw_order.
> LIST.
>
>
> v1 v2 match
>
> a  b    1
> a  c    1
> a  q    0
> b  a    1
> c  z    1
> d  f    0
> e  r    0
> z  t    0
>
>
>
>
>
> -----
> 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?"
> --
> View this message in context:
>
http://spssx-discussion.1045642.n5.nabble.com/using-loop-to-compare-text-between-two-variables-tp5727892p5727897.html
> Sent from the SPSSX Discussion mailing list archive at 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
>
> =====================
> 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





-----
--
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://spssx-discussion.1045642.n5.nabble.com/using-loop-to-compare-text-between-two-variables-tp5727892p5727910.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: using loop to compare text between two variables

David Marso
Administrator
But again I ask.  WHY bother with python for doing something so easily done with a few lines of syntax? Seriously Jon, WTF?  It seems that you will rally to python python python!! Even if a simple native solution is quite evident?
--
Jon K Peck wrote
Yes, definitely requires the Python Essentials, which are installed
standardly with Statistics since V22, and the extendedTransforms.py module
from the SPSS Community site (www.ibm.com/developerworks/spssdevcentral).

The extendedTransforms.py module, BTW, includes a number of functions that
can be used for fuzzy sorts of name comparisons that often come in handy
when dealing with variations in names, but these are a little more complex
to use.


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:   Bruce Weaver <[hidden email]>
To:     [hidden email]
Date:   11/12/2014 03:21 PM
Subject:        Re: [SPSSX-L] using loop to compare text between two
variables
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



Jon didn't say so, but I /believe/ his solution requires Python.  David's
is
old-school native SPSS syntax--NPR (i.e., No Python Required)!





Bren Tagget wrote
> David,
>
> Both of these worked. Jon I haven't tested your code yet, but David's
code
> worked beautifully. I really appreciate the help.
>
> Bren
>
>
> On Wednesday, November 12, 2014 12:44 PM, David Marso <

> david.marso@

> > wrote:
>
>
>
> Even leaner:
> DATA LIST / v1 v2 (2a1) .
> BEGIN DATA
> ab
> ac
> aq
> ba
> cz
> df
> er
> zt
> END DATA.
>
> DATASET NAME raw.
> COMPUTE raw_order=$CASENUM.
> SORT CASES BY v2.
> AGGREGATE OUTFILE * / BREAK v1 / v2=MAX(v1).
> MATCH FILES FILE raw / TABLE * /IN=match / BY v2 .
> SORT CASES BY raw_order.
> DELETE VARIABLES raw_order.
> LIST.
>
>
> v1 v2 match
>
> a  b    1
> a  c    1
> a  q    0
> b  a    1
> c  z    1
> d  f    0
> e  r    0
> z  t    0
>
>
>
>
>
> -----
> 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?"
> --
> View this message in context:
>
http://spssx-discussion.1045642.n5.nabble.com/using-loop-to-compare-text-between-two-variables-tp5727892p5727897.html

> Sent from the SPSSX Discussion mailing list archive at 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
>
> =====================
> 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





-----
--
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://spssx-discussion.1045642.n5.nabble.com/using-loop-to-compare-text-between-two-variables-tp5727892p5727910.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
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?"