Loop Syntax to Apply Same Value Labels to Non-Contiguous Variables

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

Loop Syntax to Apply Same Value Labels to Non-Contiguous Variables

james.moffitt
Suppose I have the following data.

DATA LIST LIST (",") / id (f3.0) Q1.0  Q1.1 Q1.2 Q2.0 Q2.1 Q2.3 Q3.0
Q3.1 Q3.3 (9f1.0).

BEGIN DATA

001,1,4,2,3,2,1,1,2,2

002,2,1,2,2,3,1,2,2,3

003,4,1,3,1,2,1,3,1,1

004,2,1,1,2,1,3,1,2,1

005,3,2,1,1,2,1,3,1,2

006,1,3,2,1,3,2,3,2,3

END DATA.

Does anyone know how I would create loop syntax to apply the same value
labels to a number of non-contiguous variables?

For example, suppose I want variables Q1.1, Q2.1, and Q3.1 to have the
same four value labels.

1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".

How could I create a loop to do this?

If a loop is impossible, is there another way to accomplish the
objective?

Thanks for your help.

====================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: Syntax to Apply Value Labels to Non-Contiguous Variables

Richard Ristow
At 11:19 AM 5/20/2008, [hidden email] wrote:

>how would I create loop syntax to apply the same value labels to a
>number of non-contiguous variables? For example, suppose I want
>variables Q1.1, Q2.1, and Q3.1 to have the same four value labels.
>
>1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".

You are going to have to name the variables individually. But you
shouldn't need a loop; the VALUE LABELS command can take a list of
variables. Like this (not tested):

VALUE LABELS
    Q1.1, Q2.1, and Q3.1
    1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".

=====================
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: Syntax to Apply Value Labels to Non-Contiguous Variables

james.moffitt
Thanks, Richard. I have about 70 variables in my actual data file so I
was trying to avoid having to manually enter each variable name, but
unless I reorder my variables so they are contiguous I guess that's my
only option.

-----Original Message-----
From: Richard Ristow [mailto:[hidden email]]
Sent: Tuesday, May 20, 2008 9:24 PM
To: Moffitt, James (West); [hidden email]
Subject: Re: Syntax to Apply Value Labels to Non-Contiguous Variables

At 11:19 AM 5/20/2008, [hidden email] wrote:

>how would I create loop syntax to apply the same value labels to a
>number of non-contiguous variables? For example, suppose I want
>variables Q1.1, Q2.1, and Q3.1 to have the same four value labels.
>
>1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".

You are going to have to name the variables individually. But you
shouldn't need a loop; the VALUE LABELS command can take a list of
variables. Like this (not tested):

VALUE LABELS
    Q1.1, Q2.1, and Q3.1
    1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".

=====================
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: Loop Syntax to Apply Same Value Labels to Non-Contiguous Variables

Keith McCormick
In reply to this post by james.moffitt
Hi James,

You could solve a problem like this with the GUI using "Define
Variable Properties" pretty easily, but the best way to loop as you
describe is python. The solution is just a couple lines of code, but I
am posting three sections of code to help to make it clearer. Also,
one could make this shorter, but I was trying to write for clarity.

If you haven't tried Python, your question provides a nice excuse to try it.

I hope this does the trick.

Keith
www.keithmccormick.com

DATA LIST LIST (",") / id (f3.0) Q1.0  Q1.1 Q1.2 Q2.0 Q2.1 Q2.3 Q3.0
Q3.1 Q3.3 (9f1.0).

BEGIN DATA
001,1,4,2,3,2,1,1,2,2
002,2,1,2,2,3,1,2,2,3
003,4,1,3,1,2,1,3,1,1
004,2,1,1,2,1,3,1,2,1
005,3,2,1,1,2,1,3,1,2
006,1,3,2,1,3,2,3,2,3
END DATA.

* Similar to page 116 of the SPSS-Python Integration Package Manual.
BEGIN PROGRAM python.
import spss
allvarlist=[]
varcount = spss.GetVariableCount()
for i in range(varcount):
  allvarlist.append(spss.GetVariableName(i))
print allvarlist
END PROGRAM.

* Modified in the spirit of this example as a warm up.
BEGIN PROGRAM python.
import spss
somevarlist=[]
varcount = spss.GetVariableCount()
for i in range(varcount):
     VarName = spss.GetVariableName(i)
     if (VarName[0] is "Q" and VarName[-1] is "1"):
         somevarlist.append(spss.GetVariableName(i))
print somevarlist
END PROGRAM.

* This creates the syntax and does not need any of the code above.
BEGIN PROGRAM python.
import spss
somevarlist=[]
varcount = spss.GetVariableCount()
for i in range(varcount):
     VarName = spss.GetVariableName(i)
     if (VarName[0] is "Q" and VarName[-1] is "1"):
         somevarlist.append(spss.GetVariableName(i))
command_string = "Value Labels " + ' '.join(somevarlist) + " 1 'Able'
2 'Baker' 3 'Charlie' 4 'Delta'."
print command_string
spss.Submit(command_string)
END PROGRAM.

On 5/20/08, [hidden email]
<[hidden email]> wrote:

> Suppose I have the following data.
>
> DATA LIST LIST (",") / id (f3.0) Q1.0  Q1.1 Q1.2 Q2.0 Q2.1 Q2.3 Q3.0
> Q3.1 Q3.3 (9f1.0).
>
> BEGIN DATA
>
> 001,1,4,2,3,2,1,1,2,2
>
> 002,2,1,2,2,3,1,2,2,3
>
> 003,4,1,3,1,2,1,3,1,1
>
> 004,2,1,1,2,1,3,1,2,1
>
> 005,3,2,1,1,2,1,3,1,2
>
> 006,1,3,2,1,3,2,3,2,3
>
> END DATA.
>
> Does anyone know how I would create loop syntax to apply the same value
> labels to a number of non-contiguous variables?
>
> For example, suppose I want variables Q1.1, Q2.1, and Q3.1 to have the
> same four value labels.
>
> 1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".
>
> How could I create a loop to do this?
>
> If a loop is impossible, is there another way to accomplish the
> objective?
>
> Thanks for your help.
>
> 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: Loop Syntax to Apply Same Value Labels to Non-Contiguous Variables

Oliver, Richard
Maybe I don't understand the nature of the original problem, but it's actually very simple to apply the same value labels to non-contiguous variables, since there is no requirement that the variable list on the VALUE LABELS command consist of contiguous variables. So, the simplest solution to the original problem would be:

value labels Q1.1 Q2.1 Q3.1
  1 'Able' 2 'Baker' 3 'Charlie' 4 'Delta'.

If, however, the goal is to find all variables with a common name component (such as names ending in ".1") and apply the same set of value labels to all those variables, then you need something like a Python job.

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Keith McCormick
Sent: Friday, May 23, 2008 7:46 AM
To: [hidden email]
Subject: Re: Loop Syntax to Apply Same Value Labels to Non-Contiguous Variables

Hi James,

You could solve a problem like this with the GUI using "Define
Variable Properties" pretty easily, but the best way to loop as you
describe is python. The solution is just a couple lines of code, but I
am posting three sections of code to help to make it clearer. Also,
one could make this shorter, but I was trying to write for clarity.

If you haven't tried Python, your question provides a nice excuse to try it.

I hope this does the trick.

Keith
www.keithmccormick.com

DATA LIST LIST (",") / id (f3.0) Q1.0  Q1.1 Q1.2 Q2.0 Q2.1 Q2.3 Q3.0
Q3.1 Q3.3 (9f1.0).

BEGIN DATA
001,1,4,2,3,2,1,1,2,2
002,2,1,2,2,3,1,2,2,3
003,4,1,3,1,2,1,3,1,1
004,2,1,1,2,1,3,1,2,1
005,3,2,1,1,2,1,3,1,2
006,1,3,2,1,3,2,3,2,3
END DATA.

* Similar to page 116 of the SPSS-Python Integration Package Manual.
BEGIN PROGRAM python.
import spss
allvarlist=[]
varcount = spss.GetVariableCount()
for i in range(varcount):
  allvarlist.append(spss.GetVariableName(i))
print allvarlist
END PROGRAM.

* Modified in the spirit of this example as a warm up.
BEGIN PROGRAM python.
import spss
somevarlist=[]
varcount = spss.GetVariableCount()
for i in range(varcount):
     VarName = spss.GetVariableName(i)
     if (VarName[0] is "Q" and VarName[-1] is "1"):
         somevarlist.append(spss.GetVariableName(i))
print somevarlist
END PROGRAM.

* This creates the syntax and does not need any of the code above.
BEGIN PROGRAM python.
import spss
somevarlist=[]
varcount = spss.GetVariableCount()
for i in range(varcount):
     VarName = spss.GetVariableName(i)
     if (VarName[0] is "Q" and VarName[-1] is "1"):
         somevarlist.append(spss.GetVariableName(i))
command_string = "Value Labels " + ' '.join(somevarlist) + " 1 'Able'
2 'Baker' 3 'Charlie' 4 'Delta'."
print command_string
spss.Submit(command_string)
END PROGRAM.

On 5/20/08, [hidden email]
<[hidden email]> wrote:

> Suppose I have the following data.
>
> DATA LIST LIST (",") / id (f3.0) Q1.0  Q1.1 Q1.2 Q2.0 Q2.1 Q2.3 Q3.0
> Q3.1 Q3.3 (9f1.0).
>
> BEGIN DATA
>
> 001,1,4,2,3,2,1,1,2,2
>
> 002,2,1,2,2,3,1,2,2,3
>
> 003,4,1,3,1,2,1,3,1,1
>
> 004,2,1,1,2,1,3,1,2,1
>
> 005,3,2,1,1,2,1,3,1,2
>
> 006,1,3,2,1,3,2,3,2,3
>
> END DATA.
>
> Does anyone know how I would create loop syntax to apply the same value
> labels to a number of non-contiguous variables?
>
> For example, suppose I want variables Q1.1, Q2.1, and Q3.1 to have the
> same four value labels.
>
> 1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".
>
> How could I create a loop to do this?
>
> If a loop is impossible, is there another way to accomplish the
> objective?
>
> Thanks for your help.
>
> 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: Loop Syntax to Apply Same Value Labels to Non-Contiguous Variables

Keith McCormick
It seemed to be implied in the original post that there might be a
whole bunch of these variables, and that it would be undesirable to
write them all out. Perhaps James could confirm if that was the case.
I went on the assumption we were seeking a pattern in the variable
names.


On 5/23/08, Oliver, Richard <[hidden email]> wrote:

> Maybe I don't understand the nature of the original problem, but it's actually very simple to apply the same value labels to non-contiguous variables, since there is no requirement that the variable list on the VALUE LABELS command consist of contiguous variables. So, the simplest solution to the original problem would be:
>
> value labels Q1.1 Q2.1 Q3.1
>  1 'Able' 2 'Baker' 3 'Charlie' 4 'Delta'.
>
> If, however, the goal is to find all variables with a common name component (such as names ending in ".1") and apply the same set of value labels to all those variables, then you need something like a Python job.
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Keith McCormick
> Sent: Friday, May 23, 2008 7:46 AM
> To: [hidden email]
> Subject: Re: Loop Syntax to Apply Same Value Labels to Non-Contiguous Variables
>
> Hi James,
>
> You could solve a problem like this with the GUI using "Define
> Variable Properties" pretty easily, but the best way to loop as you
> describe is python. The solution is just a couple lines of code, but I
> am posting three sections of code to help to make it clearer. Also,
> one could make this shorter, but I was trying to write for clarity.
>
> If you haven't tried Python, your question provides a nice excuse to try it.
>
> I hope this does the trick.
>
> Keith
> www.keithmccormick.com
>
> DATA LIST LIST (",") / id (f3.0) Q1.0  Q1.1 Q1.2 Q2.0 Q2.1 Q2.3 Q3.0
> Q3.1 Q3.3 (9f1.0).
>
> BEGIN DATA
> 001,1,4,2,3,2,1,1,2,2
> 002,2,1,2,2,3,1,2,2,3
> 003,4,1,3,1,2,1,3,1,1
> 004,2,1,1,2,1,3,1,2,1
> 005,3,2,1,1,2,1,3,1,2
> 006,1,3,2,1,3,2,3,2,3
> END DATA.
>
> * Similar to page 116 of the SPSS-Python Integration Package Manual.
> BEGIN PROGRAM python.
> import spss
> allvarlist=[]
> varcount = spss.GetVariableCount()
> for i in range(varcount):
>  allvarlist.append(spss.GetVariableName(i))
> print allvarlist
> END PROGRAM.
>
> * Modified in the spirit of this example as a warm up.
> BEGIN PROGRAM python.
> import spss
> somevarlist=[]
> varcount = spss.GetVariableCount()
> for i in range(varcount):
>     VarName = spss.GetVariableName(i)
>     if (VarName[0] is "Q" and VarName[-1] is "1"):
>         somevarlist.append(spss.GetVariableName(i))
> print somevarlist
> END PROGRAM.
>
> * This creates the syntax and does not need any of the code above.
> BEGIN PROGRAM python.
> import spss
> somevarlist=[]
> varcount = spss.GetVariableCount()
> for i in range(varcount):
>     VarName = spss.GetVariableName(i)
>     if (VarName[0] is "Q" and VarName[-1] is "1"):
>         somevarlist.append(spss.GetVariableName(i))
> command_string = "Value Labels " + ' '.join(somevarlist) + " 1 'Able'
> 2 'Baker' 3 'Charlie' 4 'Delta'."
> print command_string
> spss.Submit(command_string)
> END PROGRAM.
>
> On 5/20/08, [hidden email]
> <[hidden email]> wrote:
> > Suppose I have the following data.
> >
> > DATA LIST LIST (",") / id (f3.0) Q1.0  Q1.1 Q1.2 Q2.0 Q2.1 Q2.3 Q3.0
> > Q3.1 Q3.3 (9f1.0).
> >
> > BEGIN DATA
> >
> > 001,1,4,2,3,2,1,1,2,2
> >
> > 002,2,1,2,2,3,1,2,2,3
> >
> > 003,4,1,3,1,2,1,3,1,1
> >
> > 004,2,1,1,2,1,3,1,2,1
> >
> > 005,3,2,1,1,2,1,3,1,2
> >
> > 006,1,3,2,1,3,2,3,2,3
> >
> > END DATA.
> >
> > Does anyone know how I would create loop syntax to apply the same value
> > labels to a number of non-contiguous variables?
> >
> > For example, suppose I want variables Q1.1, Q2.1, and Q3.1 to have the
> > same four value labels.
> >
> > 1 "Able" 2 "Baker" 3 "Charlie" 4 "Delta".
> >
> > How could I create a loop to do this?
> >
> > If a loop is impossible, is there another way to accomplish the
> > objective?
> >
> > Thanks for your help.
> >
> > 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