assign variable labels to a string variable

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

assign variable labels to a string variable

Gerard van Meurs-2
Hello SPSS-users,

In a project that I am involved at this moment, I need to save the
variable labels of some variables into a string variable. I know about the
existence of the VALUELABEL-function, which can be used to put a value-
label in a string variable, but I need the label of the variable instead.
Is there a trick to do this?

Thanks in advance for your suggestions.

Best regards,

Gerard van Meurs

=====================
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: assign variable labels to a string variable

Jon K Peck
It is certainly possible to do this (very easy with Python), but wouldn't the data be dimensionally incompatible?  What is it you are really trying to do?

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Gerard Van Meurs <[hidden email]>
To:        [hidden email]
Date:        07/12/2011 11:36 AM
Subject:        [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hello SPSS-users,

In a project that I am involved at this moment, I need to save the
variable labels of some variables into a string variable. I know about the
existence of the VALUELABEL-function, which can be used to put a value-
label in a string variable, but I need the label of the variable instead.
Is there a trick to do this?

Thanks in advance for your suggestions.

Best regards,

Gerard van Meurs

=====================
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: assign variable labels to a string variable

Gerard van Meurs-2

The file of one of my customers is organized as follows:

 

- company1 ‘label for company 1’

- company2 ‘label for company 2’

-

- company100 ‘label for company 100’

 

All of these variables share the same value labels list: 1 = ‘yes’, 0 = ‘no’.

 

Only 1 of these 100 variables has a 1 (‘yes’), the rest is 0 (‘no’). When I loop thru these companies, I stop after finding a 1, but then I only know ‘yes’ instead of the company-name that is in the variable label.

 

In the rest of my data-processing procedure, I need the company name as a break variable to aggregate the data. Hope this clarifies why I need to put the variable label in a string variable.

 

Kind regards

 

Gerard van Meurs


Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Jon K Peck
Verzonden: dinsdag 12 juli 2011 16:02
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

 

It is certainly possible to do this (very easy with Python), but wouldn't the data be dimensionally incompatible?  What is it you are really trying to do?

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Gerard Van Meurs <[hidden email]>
To:        [hidden email]
Date:        07/12/2011 11:36 AM
Subject:        [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





Hello SPSS-users,

In a project that I am involved at this moment, I need to save the
variable labels of some variables into a string variable. I know about the
existence of the VALUELABEL-function, which can be used to put a value-
label in a string variable, but I need the label of the variable instead.
Is there a trick to do this?

Thanks in advance for your suggestions.

Best regards,

Gerard van Meurs

=====================
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: assign variable labels to a string variable

Bruce Weaver
Administrator
In reply to this post by Gerard van Meurs-2
Here's a BP method for getting a string variable that contains the variable labels for all variables in a file.  If you only want some of the variables, you could delete the unwanted variables first (DELETE VARIABLES command, for example.)

* Save the variable labels of all variables into a string variable .

new file.
dataset close all.
GET FILE = 'C:\SPSSdata\1991 U.S. General Social Survey.sav'.
dataset name rawdata.

* OMS.
DATASET DECLARE  varinfo.
OMS
  /SELECT TABLES
  /IF COMMANDS=['File Information'] SUBTYPES=['Variable Information']
  /DESTINATION FORMAT=SAV NUMBERED=tnum
   OUTFILE='varinfo' VIEWER=NO.

display dictionary. /* This is the command that does it.
OMSEND.

dataset activate varinfo.
list label. /* LABEL has the variable labels from the raw data file.

HTH.

Gerard van Meurs wrote
Hello SPSS-users,

In a project that I am involved at this moment, I need to save the
variable labels of some variables into a string variable. I know about the
existence of the VALUELABEL-function, which can be used to put a value-
label in a string variable, but I need the label of the variable instead.
Is there a trick to do this?

Thanks in advance for your suggestions.

Best regards,

Gerard van Meurs

=====================
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: assign variable labels to a string variable

David Marso
Administrator
In reply to this post by Gerard van Meurs-2
"In the rest of my data-processing procedure, I need the company name as a break variable to aggregate the data. Hope this clarifies why I need to put the variable label in a string variable."
You are doing case level 'looping' then creating a global 'file level' additional analysis (for some reason I don't comprehend).  It would be useful if you were to be more specific as to how the additional analysis is related to the 'trigger' of locating a 1 in a single field for one case and how the aggregate is initiated.  I don't see how knowing what the variable label for such field advances your solution.  What does the term 'dimensionally incompatible' -From Jon Peck's reply - mean to you.  I am at a loss to what you are really attempting to achieve and more information is necessary.  It is surely possible to obtain the Variable labels via OMS (Bruce) or scripting but your idea of "assigning them to a string variable" is incomprehensible.  Are you looking for a QAD.  
Go into variable view..
Copy Paste the Variable Labels.
Create a VALUE LABELS command to map them to 1... 100.
In your LOOP...create a new variable which stores the index of the '1'.
Apply the LABELS..
OTOH you can do this without copy/paste but I'll leave that to you to hack together some OMS or Python.
HTH, David




Gerard van Meurs wrote
The file of one of my customers is organized as follows:

 

- company1 'label for company 1'

- company2 'label for company 2'

-

- company100 'label for company 100'

 

All of these variables share the same value labels list: 1 = 'yes', 0 =
'no'.

 

Only 1 of these 100 variables has a 1 ('yes'), the rest is 0 ('no').
When I loop thru these companies, I stop after finding a 1, but then I
only know 'yes' instead of the company-name that is in the variable
label.

 

In the rest of my data-processing procedure, I need the company name as
a break variable to aggregate the data. Hope this clarifies why I need
to put the variable label in a string variable.

 

Kind regards

 

Gerard van Meurs

________________________________

Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Jon K
Peck
Verzonden: dinsdag 12 juli 2011 16:02
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

 

It is certainly possible to do this (very easy with Python), but
wouldn't the data be dimensionally incompatible?  What is it you are
really trying to do?

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Gerard Van Meurs <[hidden email]>
To:        [hidden email] 
Date:        07/12/2011 11:36 AM
Subject:        [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>

________________________________




Hello SPSS-users,

In a project that I am involved at this moment, I need to save the
variable labels of some variables into a string variable. I know about
the
existence of the VALUELABEL-function, which can be used to put a value-
label in a string variable, but I need the label of the variable
instead.
Is there a trick to do this?

Thanks in advance for your suggestions.

Best regards,

Gerard van Meurs

=====================
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: assign variable labels to a string variable

Rich Ulrich
Right now, he has 100 No/Yes variables, with variable labels for
vars 1-100, each of which is a Company.   "1"  is "Yes" for a case.

In order to use Aggregate effectively, he needs *one*  variable that
has values from 1 to 100, with value labels for each company.

He *seems*  to be asking for a string variable with 100 names,
but I think this is a mistaken way to read it.  Or a mistake in the asking.
Or something you can create if you have a 1-100 set of value-labels.

He already has a loop that he uses to find a *number* to represent
the Yes in his list.  What he needs (I think)  is a value-label list for this variable.
 -- I would probably List my 100 variables, with labels, and use a text editor
to change this to a variable name/value list declaration, since it only has to
be done once.

But there are probably other ways to do it.

> Date: Tue, 12 Jul 2011 08:29:19 -0700
> From: [hidden email]
> Subject: Re: assign variable labels to a string variable
> To: [hidden email]
>
> "In the rest of my data-processing procedure, I need the company name as a
> break variable to aggregate the data. Hope this clarifies why I need to put
> the variable label in a string variable."

[snip, rest]

--
Rich Ulrich


Reply | Threaded
Open this post in threaded view
|

Re: assign variable labels to a string variable

David Marso
Administrator
Truly EXCELLENT example of InterneTelepathy Rich!
By the time I got most of the way through with my post I had deduced that the LOOP would *Hopefully* store the *index* of the *location* where the 1 is found and that a simple copy/paste of the labels from *variable view* would facilitate the creation of a value label statement for the *index* variable ;-)
Sometimes the Socratic method sucks ;-)
---
Rich Ulrich-2 wrote
Right now, he has 100 No/Yes variables, with variable labels for

vars 1-100, each of which is a Company.   "1"  is "Yes" for a case.


In order to use Aggregate effectively, he needs *one*  variable that
has values from 1 to 100, with value labels for each company.

He *seems*  to be asking for a string variable with 100 names,
but I think this is a mistaken way to read it.  Or a mistake in the asking.
Or something you can create if you have a 1-100 set of value-labels.

He already has a loop that he uses to find a *number* to represent
the Yes in his list.  What he needs (I think)  is a value-label list for this variable.
 -- I would probably List my 100 variables, with labels, and use a text editor
to change this to a variable name/value list declaration, since it only has to
be done once.

But there are probably other ways to do it.

> Date: Tue, 12 Jul 2011 08:29:19 -0700
> From: [hidden email]
> Subject: Re: assign variable labels to a string variable
> To: [hidden email]
>
> "In the rest of my data-processing procedure, I need the company name as a
> break variable to aggregate the data. Hope this clarifies why I need to put
> the variable label in a string variable."

[snip, rest]

--
Rich Ulrich
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: assign variable labels to a string variable

Gerard van Meurs-2
In reply to this post by Bruce Weaver
Hello Bruce,


Thanks for your suggestion. I think that this may be a solution for my
problem. I never realised that the use of OMS is so versatile.

Kind regards, Gerard van Meurs
-----Oorspronkelijk bericht-----
Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Bruce
Weaver
Verzonden: dinsdag 12 juli 2011 16:30
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

Here's a BP method for getting a string variable that contains the
variable
labels for all variables in a file.  If you only want some of the
variables,
you could delete the unwanted variables first (DELETE VARIABLES command,
for
example.)

* Save the variable labels of all variables into a string variable .

new file.
dataset close all.
GET FILE = 'C:\SPSSdata\1991 U.S. General Social Survey.sav'.
dataset name rawdata.

* OMS.
DATASET DECLARE  varinfo.
OMS
  /SELECT TABLES
  /IF COMMANDS=['File Information'] SUBTYPES=['Variable Information']
  /DESTINATION FORMAT=SAV NUMBERED=tnum
   OUTFILE='varinfo' VIEWER=NO.

display dictionary. /* This is the command that does it.
OMSEND.

dataset activate varinfo.
list label. /* LABEL has the variable labels from the raw data file.

HTH.


Gerard van Meurs wrote:
>
> Hello SPSS-users,
>
> In a project that I am involved at this moment, I need to save the
> variable labels of some variables into a string variable. I know about
the
> existence of the VALUELABEL-function, which can be used to put a
value-
> label in a string variable, but I need the label of the variable
instead.

> Is there a trick to do this?
>
> Thanks in advance for your suggestions.
>
> Best regards,
>
> Gerard van Meurs
>
> =====================
> 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
[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/assign-variable-labels-to-
a-string-variable-tp4578333p4579064.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: assign variable labels to a string variable

Gerard van Meurs-2
In reply to this post by David Marso
Hi David and Ulrich,

Thanks for thinking with me, but I'm afraid that I question was not
specific enough. My customer delivers me file in a certain format. I
need to automate certain tasks, and I don't like to edit the labels in a
text-editor.

As I walk through the loop of 100 variables, I can easily create a new
variable (say: company) with the values of 1 thru 100. But suppose var21
contains a 1 (labeled 'yes'), and suppose that var21 has the label
'Microsoft Corporation'. Than I still need something to transfer the
variable label of var21 ('Microsoft Corporation' to the label 21 of my
new variable company.

The OMS-suggestion from Bruce is probably a solution, but I hoped for
something simpler.

Kinds regards, Gerard van Meurs

-----Oorspronkelijk bericht-----
Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens David
Marso
Verzonden: dinsdag 12 juli 2011 22:09
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

Truly EXCELLENT example of InterneTelepathy Rich!
By the time I got most of the way through with my post I had deduced
that
the LOOP would *Hopefully* store the *index* of the *location* where the
1
is found and that a simple copy/paste of the labels from *variable view*
would facilitate the creation of a value label statement for the *index*
variable ;-)
Sometimes the Socratic method sucks ;-)
---

Rich Ulrich-2 wrote:

>
> Right now, he has 100 No/Yes variables, with variable labels for
>
> vars 1-100, each of which is a Company.   "1"  is "Yes" for a case.
>
>
> In order to use Aggregate effectively, he needs *one*  variable that
> has values from 1 to 100, with value labels for each company.
>
> He *seems*  to be asking for a string variable with 100 names,
> but I think this is a mistaken way to read it.  Or a mistake in the
> asking.
> Or something you can create if you have a 1-100 set of value-labels.
>
> He already has a loop that he uses to find a *number* to represent
> the Yes in his list.  What he needs (I think)  is a value-label list
for
> this variable.
>  -- I would probably List my 100 variables, with labels, and use a
text
> editor
> to change this to a variable name/value list declaration, since it
only

> has to
> be done once.
>
> But there are probably other ways to do it.
>
>> Date: Tue, 12 Jul 2011 08:29:19 -0700
>> From: [hidden email]
>> Subject: Re: assign variable labels to a string variable
>> To: [hidden email]
>>
>> "In the rest of my data-processing procedure, I need the company name
as
>> a
>> break variable to aggregate the data. Hope this clarifies why I need
to
>> put
>> the variable label in a string variable."
>
> [snip, rest]
>
> --
> Rich Ulrich
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-
a-string-variable-tp4578333p4580129.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: assign variable labels to a string variable

David Marso
Administrator
Looks like you simply need to map your vector indexes into the variable labels.
That could be easily achieved in Python using the spssaux library.
I cannot provide specifics as my SPSS 11.5 does not support python.
Seems you have something like...

VECTOR V=var001 TO var100.
COMPUTE Company=0.
LOOP #=1 TO 100.
IF V(#)=1 Company=#.
END LOOP IF Company GT 0.

Then the idea is to create a VALUE LABELS command for Company using Variable Labels from var001 to var100 depending upon value of company mapping to the index of the vector....
Something like the following obviously untested and probably incorrect python code.
(I hacked it together from several different places and have no way to test it).
The basic spirit of the code is sound but likely has a brain fart or two.
Play around with it and you can probably get it to work.  It is certainly more elegant than OMS or copy/paste.  Please post a working version after you fix it ;-)
OTOH it might work right out of the gate?
---
BEGIN PROGRAM.
vectdict = spssaux.VariableDict()
vectvars = vardict.range(start="var001", end="var100")
cmd="VALUE LABELS Company "
x=1
for v in vectvars:
   lbl=v.VariableLabel
   cmd += x + "'" + lbl + "'  "
   x=x+1
spss.submit (cmd)
END PROGRAM.
----
Gerard van Meurs wrote
Hi David and Ulrich,

Thanks for thinking with me, but I'm afraid that I question was not
specific enough. My customer delivers me file in a certain format. I
need to automate certain tasks, and I don't like to edit the labels in a
text-editor.

As I walk through the loop of 100 variables, I can easily create a new
variable (say: company) with the values of 1 thru 100. But suppose var21
contains a 1 (labeled 'yes'), and suppose that var21 has the label
'Microsoft Corporation'. Than I still need something to transfer the
variable label of var21 ('Microsoft Corporation' to the label 21 of my
new variable company.

The OMS-suggestion from Bruce is probably a solution, but I hoped for
something simpler.

Kinds regards, Gerard van Meurs

-----Oorspronkelijk bericht-----
Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens David
Marso
Verzonden: dinsdag 12 juli 2011 22:09
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

Truly EXCELLENT example of InterneTelepathy Rich!
By the time I got most of the way through with my post I had deduced
that
the LOOP would *Hopefully* store the *index* of the *location* where the
1
is found and that a simple copy/paste of the labels from *variable view*
would facilitate the creation of a value label statement for the *index*
variable ;-)
Sometimes the Socratic method sucks ;-)
---

Rich Ulrich-2 wrote:
>
> Right now, he has 100 No/Yes variables, with variable labels for
>
> vars 1-100, each of which is a Company.   "1"  is "Yes" for a case.
>
>
> In order to use Aggregate effectively, he needs *one*  variable that
> has values from 1 to 100, with value labels for each company.
>
> He *seems*  to be asking for a string variable with 100 names,
> but I think this is a mistaken way to read it.  Or a mistake in the
> asking.
> Or something you can create if you have a 1-100 set of value-labels.
>
> He already has a loop that he uses to find a *number* to represent
> the Yes in his list.  What he needs (I think)  is a value-label list
for
> this variable.
>  -- I would probably List my 100 variables, with labels, and use a
text
> editor
> to change this to a variable name/value list declaration, since it
only
> has to
> be done once.
>
> But there are probably other ways to do it.
>
>> Date: Tue, 12 Jul 2011 08:29:19 -0700
>> From: [hidden email]
>> Subject: Re: assign variable labels to a string variable
>> To: [hidden email]
>>
>> "In the rest of my data-processing procedure, I need the company name
as
>> a
>> break variable to aggregate the data. Hope this clarifies why I need
to
>> put
>> the variable label in a string variable."
>
> [snip, rest]
>
> --
> Rich Ulrich
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-
a-string-variable-tp4578333p4580129.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?"
Reply | Threaded
Open this post in threaded view
|

Re: assign variable labels to a string variable

Jon K Peck
In reply to this post by Gerard van Meurs-2
It is easy to do this with Python.

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
vardict['company'].VariableLabel = vardict[21].VariableLabel
end program.

There are various ways to replace 21 with the parameter you need, but the easiest would probaby be to use programmability for the whole thing.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Gerard van Meurs <[hidden email]>
To:        [hidden email]
Date:        07/13/2011 05:10 PM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi David and Ulrich,

Thanks for thinking with me, but I'm afraid that I question was not
specific enough. My customer delivers me file in a certain format. I
need to automate certain tasks, and I don't like to edit the labels in a
text-editor.

As I walk through the loop of 100 variables, I can easily create a new
variable (say: company) with the values of 1 thru 100. But suppose var21
contains a 1 (labeled 'yes'), and suppose that var21 has the label
'Microsoft Corporation'. Than I still need something to transfer the
variable label of var21 ('Microsoft Corporation' to the label 21 of my
new variable company.

The OMS-suggestion from Bruce is probably a solution, but I hoped for
something simpler.

Kinds regards, Gerard van Meurs

-----Oorspronkelijk bericht-----
Van: SPSSX(r) Discussion [
[hidden email]] Namens David
Marso
Verzonden: dinsdag 12 juli 2011 22:09
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

Truly EXCELLENT example of InterneTelepathy Rich!
By the time I got most of the way through with my post I had deduced
that
the LOOP would *Hopefully* store the *index* of the *location* where the
1
is found and that a simple copy/paste of the labels from *variable view*
would facilitate the creation of a value label statement for the *index*
variable ;-)
Sometimes the Socratic method sucks ;-)
---

Rich Ulrich-2 wrote:
>
> Right now, he has 100 No/Yes variables, with variable labels for
>
> vars 1-100, each of which is a Company.   "1"  is "Yes" for a case.
>
>
> In order to use Aggregate effectively, he needs *one*  variable that
> has values from 1 to 100, with value labels for each company.
>
> He *seems*  to be asking for a string variable with 100 names,
> but I think this is a mistaken way to read it.  Or a mistake in the
> asking.
> Or something you can create if you have a 1-100 set of value-labels.
>
> He already has a loop that he uses to find a *number* to represent
> the Yes in his list.  What he needs (I think)  is a value-label list
for
> this variable.
>  -- I would probably List my 100 variables, with labels, and use a
text
> editor
> to change this to a variable name/value list declaration, since it
only
> has to
> be done once.
>
> But there are probably other ways to do it.
>
>> Date: Tue, 12 Jul 2011 08:29:19 -0700
>> From: [hidden email]
>> Subject: Re: assign variable labels to a string variable
>> To: [hidden email]
>>
>> "In the rest of my data-processing procedure, I need the company name
as
>> a
>> break variable to aggregate the data. Hope this clarifies why I need
to
>> put
>> the variable label in a string variable."
>
> [snip, rest]
>
> --
> Rich Ulrich
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-
a-string-variable-tp4578333p4580129.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: assign variable labels to a string variable

Albert-Jan Roskam
Hoi Gerard,

You mean something like this?

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(vardict):
    varlbls[val] = v.VariableLabel
end program.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Wed, 7/13/11, Jon K Peck <[hidden email]> wrote:

From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Wednesday, July 13, 2011, 6:22 PM

It is easy to do this with Python.

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
vardict['company'].VariableLabel = vardict[21].VariableLabel
end program.

There are various ways to replace 21 with the parameter you need, but the easiest would probaby be to use programmability for the whole thing.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Gerard van Meurs <[hidden email]>
To:        [hidden email]
Date:        07/13/2011 05:10 PM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Hi David and Ulrich,

Thanks for thinking with me, but I'm afraid that I question was not
specific enough. My customer delivers me file in a certain format. I
need to automate certain tasks, and I don't like to edit the labels in a
text-editor.

As I walk through the loop of 100 variables, I can easily create a new
variable (say: company) with the values of 1 thru 100. But suppose var21
contains a 1 (labeled 'yes'), and suppose that var21 has the label
'Microsoft Corporation'. Than I still need something to transfer the
variable label of var21 ('Microsoft Corporation' to the label 21 of my
new variable company.

The OMS-suggestion from Bruce is probably a solution, but I hoped for
something simpler.

Kinds regards, Gerard van Meurs

-----Oorspronkelijk bericht-----
Van: SPSSX(r) Discussion [
mailto:SPSSX-L@...] Namens David
Marso
Verzonden: dinsdag 12 juli 2011 22:09
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

Truly EXCELLENT example of InterneTelepathy Rich!
By the time I got most of the way through with my post I had deduced
that
the LOOP would *Hopefully* store the *index* of the *location* where the
1
is found and that a simple copy/paste of the labels from *variable view*
would facilitate the creation of a value label statement for the *index*
variable ;-)
Sometimes the Socratic method sucks ;-)
---

Rich Ulrich-2 wrote:
>
> Right now, he has 100 No/Yes variables, with variable labels for
>
> vars 1-100, each of which is a Company.   "1"  is "Yes" for a case.
>
>
> In order to use Aggregate effectively, he needs *one*  variable that
> has values from 1 to 100, with value labels for each company.
>
> He *seems*  to be asking for a string variable with 100 names,
> but I think this is a mistaken way to read it.  Or a mistake in the
> asking.
> Or something you can create if you have a 1-100 set of value-labels.
>
> He already has a loop that he uses to find a *number* to represent
> the Yes in his list.  What he needs (I think)  is a value-label list
for
> this variable.
>  -- I would probably List my 100 variables, with labels, and use a
text
> editor
> to change this to a variable name/value list declaration, since it
only
> has to
> be done once.
>
> But there are probably other ways to do it.
>
>> Date: Tue, 12 Jul 2011 08:29:19 -0700
>> From: [hidden email]
>> Subject: Re: assign variable labels to a string variable
>> To: [hidden email]
>>
>> "In the rest of my data-processing procedure, I need the company name
as
>> a
>> break variable to aggregate the data. Hope this clarifies why I need
to
>> put
>> the variable label in a string variable."
>
> [snip, rest]
>
> --
> Rich Ulrich
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-
a-string-variable-tp4578333p4580129.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: assign variable labels to a string variable

David Marso
Administrator
VERY NICE Albert-Jan!!!
I didn't know one could do " varlbls[val] = v.VariableLabel"
But, unless the VECTOR variables are the first in the file they won't correspond to the indexes in vardict.
Combining what I posted previously (without my hideous cmd crap ;-)

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
varvect=vardict.range(start="var001", end="var100")
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(varvect):
    varlbls[val] = v.VariableLabel
end program.

Question:  Will the following work as well or would we be pushing the envelop?

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for val, v in enumerate(vardict.range(start="var001", end="var100")):
   vardict['Company'].ValueLabels [val] = v.VariableLabel
end program.
------------------------------
Albert-Jan Roskam wrote
Hoi Gerard,

You mean something like this?

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(vardict):
    varlbls[val] = v.VariableLabel
end program.

Cheers!!

Albert-Jan


<SNIP remaining quoted posts etc>
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: assign variable labels to a string variable

Jon K Peck
VariableDict returns a Python dictionary, so the order of the entries in it (variables) is arbitrary and will not match the SPSS dictionary order.  It is necessary, therefore, to use the variable names as indexes or to use the range method when order matters, which is rarely.  (vardict.range() will give the entire variable list in SPSS dictionary order.)

In the first program below, the value labels for variable company are fetched but never used.

The second program won't work as written.  The valueLabels property is a dictionary where the keys are the labelled values and the values are the labels.  You would need to construct the whole value label dictionary and then assign that dictionary to the ValueLabels property.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        07/14/2011 01:48 AM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




VERY NICE Albert-Jan!!!
I didn't know one could do " varlbls[val] = v.VariableLabel"
But, unless the VECTOR variables are the first in the file they won't
correspond to the indexes in vardict.
Combining what I posted previously (without my hideous cmd crap ;-)

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
varvect=vardict.range(start="var001", end="var100")
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(varvect):
   varlbls[val] = v.VariableLabel
end program.

Question:  Will the following work as well or would we be pushing the
envelop?

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for val, v in enumerate(vardict.range(start="var001", end="var100")):
  vardict['Company'].ValueLabels [val] = v.VariableLabel
end program.
------------------------------

Albert-Jan Roskam wrote:
>
> Hoi Gerard,
>
> You mean something like this?
>
> begin program.
> """ Transfer the variable label of variableN
> to value label N of a target variable."""
> import spss, spssaux
> vardict = spssaux.VariableDict()
> vallbls = vardict['Company'].ValueLabels
> for val, v in enumerate(vardict):
>     varlbls[val] = v.VariableLabel
> end program.
>
> Cheers!!
>
> Albert-Jan
>
>
> <SNIP remaining quoted posts etc>
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-a-string-variable-tp4578333p4584712.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

Reply | Threaded
Open this post in threaded view
|

Re: assign variable labels to a string variable

Albert-Jan Roskam
Hi Gerard, Jon,

Ok, the frist error was a typo (brain freeze after 9h of programming in R). I hope the *untested* code below tackles both this error and the 'unorderedness' dictionary problem of the previous code.

import spss, spssaux
vardict = spssaux.VariableDict()
vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var001", end="var100"))])
vardict['Company'].ValueLabels = vallabels

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 7/14/11, Jon K Peck <[hidden email]> wrote:

From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Thursday, July 14, 2011, 8:53 AM

VariableDict returns a Python dictionary, so the order of the entries in it (variables) is arbitrary and will not match the SPSS dictionary order.  It is necessary, therefore, to use the variable names as indexes or to use the range method when order matters, which is rarely.  (vardict.range() will give the entire variable list in SPSS dictionary order.)

In the first program below, the value labels for variable company are fetched but never used.

The second program won't work as written.  The valueLabels property is a dictionary where the keys are the labelled values and the values are the labels.  You would need to construct the whole value label dictionary and then assign that dictionary to the ValueLabels property.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        07/14/2011 01:48 AM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




VERY NICE Albert-Jan!!!
I didn't know one could do " varlbls[val] = v.VariableLabel"
But, unless the VECTOR variables are the first in the file they won't
correspond to the indexes in vardict.
Combining what I posted previously (without my hideous cmd crap ;-)

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
varvect=vardict.range(start="var001", end="var100")
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(varvect):
   varlbls[val] = v.VariableLabel
end program.

Question:  Will the following work as well or would we be pushing the
envelop?

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for val, v in enumerate(vardict.range(start="var001", end="var100")):
  vardict['Company'].ValueLabels [val] = v.VariableLabel
end program.
------------------------------

Albert-Jan Roskam wrote:
>
> Hoi Gerard,
>
> You mean something like this?
>
> begin program.
> """ Transfer the variable label of variableN
> to value label N of a target variable."""
> import spss, spssaux
> vardict = spssaux.VariableDict()
> vallbls = vardict['Company'].ValueLabels
> for val, v in enumerate(vardict):
>     varlbls[val] = v.VariableLabel
> end program.
>
> Cheers!!
>
> Albert-Jan
>
>
> <SNIP remaining quoted posts etc>
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-a-string-variable-tp4578333p4584712.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

Reply | Threaded
Open this post in threaded view
|

Re: assign variable labels to a string variable

Albert-Jan Roskam
In reply to this post by Jon K Peck
Hi Gerard, Jon,

Ok, the frist error was a typo (brain freeze after 9h of programming in R). I hope the *untested* code below tackles both this error and the 'unorderedness' dictionary problem of the previous code.

import spss, spssaux
vardict = spssaux.VariableDict()
vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var001", end="var100"))])
vardict['Company'].ValueLabels = vallabels

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 7/14/11, Jon K Peck <[hidden email]> wrote:

From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Thursday, July 14, 2011, 8:53 AM

VariableDict returns a Python dictionary, so the order of the entries in it (variables) is arbitrary and will not match the SPSS dictionary order.  It is necessary, therefore, to use the variable names as indexes or to use the range method when order matters, which is rarely.  (vardict.range() will give the entire variable list in SPSS dictionary order.)

In the first program below, the value labels for variable company are fetched but never used.

The second program won't work as written.  The valueLabels property is a dictionary where the keys are the labelled values and the values are the labels.  You would need to construct the whole value label dictionary and then assign that dictionary to the ValueLabels property.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        07/14/2011 01:48 AM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




VERY NICE Albert-Jan!!!
I didn't know one could do " varlbls[val] = v.VariableLabel"
But, unless the VECTOR variables are the first in the file they won't
correspond to the indexes in vardict.
Combining what I posted previously (without my hideous cmd crap ;-)

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
varvect=vardict.range(start="var001", end="var100")
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(varvect):
   varlbls[val] = v.VariableLabel
end program.

Question:  Will the following work as well or would we be pushing the
envelop?

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for val, v in enumerate(vardict.range(start="var001", end="var100")):
  vardict['Company'].ValueLabels [val] = v.VariableLabel
end program.
------------------------------

Albert-Jan Roskam wrote:
>
> Hoi Gerard,
>
> You mean something like this?
>
> begin program.
> """ Transfer the variable label of variableN
> to value label N of a target variable."""
> import spss, spssaux
> vardict = spssaux.VariableDict()
> vallbls = vardict['Company'].ValueLabels
> for val, v in enumerate(vardict):
>     varlbls[val] = v.VariableLabel
> end program.
>
> Cheers!!
>
> Albert-Jan
>
>
> <SNIP remaining quoted posts etc>
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-a-string-variable-tp4578333p4584712.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

Reply | Threaded
Open this post in threaded view
|

Re: assign variable labels to a string variable

Gerard van Meurs-2

Hoi Albert-Jan,

 

Thanks for your advice. Although I’m not familiar with Python, this seems to be a nice solution.

 

There is however still 1 error when I run the script. Below my (test-)script, and below that the error-message.

 

*

* read in dummy-data

*.

data list free /var1 to var10.

begin data

1 0 0 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 0 1 0 0 0

0 0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 1

end data.

 

*

* label the variables

*.

variable labels

 var1 'bedrijf 1'

 var2 'bedrijf 2'

 var3 'bedrijf 3'

 var4 'bedrijf 4'

 var5 'bedrijf 5'

 var6 'bedrijf 6'

 var7 'bedrijf 7'

 var8 'bedrijf 8'

 var9 'bedrijf 9'

 var10 'bedrijf 10'.

 

*

* fill company-variable

*.

vector varlist = var1 to var10.

compute company = 0.

loop #i=1 to 10.

. do if (varlist(#i) = 1).

.  compute company = #i.

. end if.

end loop if (company = 1).

 

freq var = company.

 

*

* label the company-values with the variable labels of var1 to var10

*.

begin program.

import spss, spssaux

vardict = spssaux.VariableDict()

vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])

vardict['company'].ValueLabels = vallabels

end program.

 

freq var = company.

 

 

And this is the error message after running the script; any idea what I should change?

 

begin program.

import spss, spssaux

vardict = spssaux.VariableDict()

vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])

vardict['company'].ValueLabels = vallabels

end program.

Traceback (most recent call last):

  File "<string>", line 4, in <module>

AttributeError: 'str' object has no attribute 'VariableLabel'

 

freq var = company.

 

 

Kind regards,

 

Gerard van Meurs


Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Albert-Jan Roskam
Verzonden: donderdag 14 juli 2011 14:17
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

 

Hi Gerard, Jon,

Ok, the frist error was a typo (brain freeze after 9h of programming in R). I hope the *untested* code below tackles both this error and the 'unorderedness' dictionary problem of the previous code.

import spss, spssaux
vardict = spssaux.VariableDict()
vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var001", end="var100"))])
vardict['Company'].ValueLabels = vallabels

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 7/14/11, Jon K Peck <[hidden email]> wrote:


From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Thursday, July 14, 2011, 8:53 AM

VariableDict returns a Python dictionary, so the order of the entries in it (variables) is arbitrary and will not match the SPSS dictionary order.  It is necessary, therefore, to use the variable names as indexes or to use the range method when order matters, which is rarely.  (vardict.range() will give the entire variable list in SPSS dictionary order.)

In the first program below, the value labels for variable company are fetched but never used.

The second program won't work as written.  The valueLabels property is a dictionary where the keys are the labelled values and the values are the labels.  You would need to construct the whole value label dictionary and then assign that dictionary to the ValueLabels property.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        07/14/2011 01:48 AM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





VERY NICE Albert-Jan!!!
I didn't know one could do " varlbls[val] = v.VariableLabel"
But, unless the VECTOR variables are the first in the file they won't
correspond to the indexes in vardict.
Combining what I posted previously (without my hideous cmd crap ;-)

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
varvect=vardict.range(start="var001", end="var100")
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(varvect):
   varlbls[val] = v.VariableLabel
end program.

Question:  Will the following work as well or would we be pushing the
envelop?

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for val, v in enumerate(vardict.range(start="var001", end="var100")):
  vardict['Company'].ValueLabels [val] = v.VariableLabel
end program.
------------------------------

Albert-Jan Roskam wrote:
>
> Hoi Gerard,
>
> You mean something like this?
>
> begin program.
> """ Transfer the variable label of variableN
> to value label N of a target variable."""
> import spss, spssaux
> vardict = spssaux.VariableDict()
> vallbls = vardict['Company'].ValueLabels
> for val, v in enumerate(vardict):
>     varlbls[val] = v.VariableLabel
> end program.
>
> Cheers!!
>
> Albert-Jan
>
>
> <SNIP remaining quoted posts etc>
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-a-string-variable-tp4578333p4584712.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

 

Reply | Threaded
Open this post in threaded view
|

Re: assign variable labels to a string variable

Albert-Jan Roskam
Hoi Gerard,

Well, now I'm finally behind a computer with Python and Spss. The code below works.
vardict.range apparently returns a list, I looped over the list items, which are strings, and those strings have no ValueLabel method.

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
vallabels = dict([(n+1, vardict[v].VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])
vardict['company'].ValueLabels = vallabels
end program.

fre company.


Cheers!!
Albert-Jan

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAccAAAEoCAIAAACijK2yAAAXSklEQVR4nO2b3ZqsuK4E6/1fmrlY09U02IltBLakiKsaFz/KMJXNXuc7nw0AAOz4zB4AACAUtCoAgCW0KgCAJbQqAIAltCoAgCW0KgCAJbQqAIAltCoAgCW0KgCAJbQqPMtnh1j8fi4u1g5ovFTxlMcSQ3Z4tuBB2ntz34znz2LxfK/ipWhVeA2eLXiQYn/df0UttmRLa9dGAjCExwse5IVW1b1Jq8L78HjBgzzdqgOLtCo8DY8XPMjNKjwf0PVaWvxMpcLT8ITBs3x2iMX2d1VxKVoVVoAnDHxwvxCpVHgHHjLwwZ1O5C0V3oTnDADAEloVAMASWhUAwBJaFQDAEloVAMASWhUAwJLy/zchAAD0UmjV2SMBAPim3KpPvhpnJ4nhMBnDBLECIQJadQ5JDIfJGCaIFQgR0KpzSGI4TMYwQaxAiIBWnUMSw2EyhglixbCQ/T87PoG+8vfbRzeUVp1DEsNhMoYJYsWYkP1ZDyltbNVHoVXnkMRwmIxhglgxIOR8SvHN8bC4/6UcfjXirP3xh9O/lxVXuPnzpFXnkMRwmIxhglhh0qrFr4odevisz2r/9nyMyds0rTqHJIbDZAwTxIp3WnXggOJdROceypRWdUwSw2EyhglihYtW/eyoHXZ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wCKteoBWdQFCvtCqI9Q07dFHikVa9QCt6gKEfKFVR7jU1H4krXoJreoChHyhVUcovo1ediWtOgat6gKEfKFVR/g/wGnmzw/nxe1KaG+r1m4nZnZkeI+LVt1K23H43LUF7jYLIV8af6qGfiK06hM8HT+J4aUy3hlmqSBWIEQznJFWLfBC9iSGl8pIiRxAiIZWdUYSw2EyhgliBUIEtOockhgOkzFMECsQIqBV55DEcJiMYYJYgRABrTqHJIbDZAwTxAqECGjVOSQxHCZjmCBWIERAq84hieEwGcMEsQIhAlp1DkkMh8kYJogVCBHQqnNIYjhMxjBBrECIgFadQxLDYTKGCWIFQgS06hySGA6TMUwQKxAicN+qnxLiYH2pBwas3mt9w+cJLw1fXuE+jVPpUVcIYgVCVsN9q35pGZhWbaf4syx+1heZMpUedYUgViBkQSK36uHPr/7P4hWeY3HD/z8Ti7Vq+1RJSgQhaxK2VfWzdfnkPY0Lw6u1avGa lAhCViNpqxaPpFUP0Kq9h00BIauRt1U/O4pXeBQXhmnV3sOmgJDVSNqq/AtAC7Rq72FTQMhqZG9V3lUFtGrvYVNAyGqEbdVt97/xDyuHz7RqjRalvVe4T+NUxd3Xp3TddB0QshpxWtUXSQwvlfHOMEsFsQIhD0GrziGJ4aUyUiIHEPIQtOockhgOkzFMECsQIqBV55DEcJiMYYJYgRABrTqHJIbDZAwTxAqECGjVOSQxHCZjmCBWIERAq84hieEwGcMEsQIhAtWqAAAwBq0KAGDJsVUBAOAm/wHH9YXbrbUMUAAAAABJRU5ErkJggg==" alt="">

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 7/15/11, Gerard van Meurs <[hidden email]> wrote:

From: Gerard van Meurs <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Friday, July 15, 2011, 11:48 AM

Hoi Albert-Jan,

 

Thanks for your advice. Although I’m not familiar with Python, this seems to be a nice solution.

 

There is however still 1 error when I run the script. Below my (test-)script, and below that the error-message.

 

*

* read in dummy-data

*.

data list free /var1 to var10.

begin data

1 0 0 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 0 1 0 0 0

0 0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 1

end data.

 

*

* label the variables

*.

variable labels

 var1 'bedrijf 1'

 var2 'bedrijf 2'

 var3 'bedrijf 3'

 var4 'bedrijf 4'

 var5 'bedrijf 5'

 var6 'bedrijf 6'

 var7 'bedrijf 7'

 var8 'bedrijf 8'

 var9 'bedrijf 9'

 var10 'bedrijf 10'.

 

*

* fill company-variable

*.

vector varlist = var1 to var10.

compute company = 0.

loop #i=1 to 10.

. do if (varlist(#i) = 1).

.  compute company = #i.

. end if.

end loop if (company = 1).

 

freq var = company.

 

*

* label the company-values with the variable labels of var1 to var10

*.

begin program.

import spss, spssaux

vardict = spssaux.VariableDict()

vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])

vardict['company'].ValueLabels = vallabels

end program.

 

freq var = company.

 

 

And this is the error message after running the script; any idea what I should change?

 

begin program.

import spss, spssaux

vardict = spssaux.VariableDict()

vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])

vardict['company'].ValueLabels = vallabels

end program.

Traceback (most recent call last):

  File "<string>", line 4, in <module>

AttributeError: 'str' object has no attribute 'VariableLabel'

 

freq var = company.

 

 

Kind regards,

 

Gerard van Meurs


Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Albert-Jan Roskam
Verzonden: donderdag 14 juli 2011 14:17
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

 

Hi Gerard, Jon,

Ok, the frist error was a typo (brain freeze after 9h of programming in R). I hope the *untested* code below tackles both this error and the 'unorderedness' dictionary problem of the previous code.

import spss, spssaux
vardict = spssaux.VariableDict()
vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var001", end="var100"))])
vardict['Company'].ValueLabels = vallabels

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 7/14/11, Jon K Peck <[hidden email]> wrote:


From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Thursday, July 14, 2011, 8:53 AM

VariableDict returns a Python dictionary, so the order of the entries in it (variables) is arbitrary and will not match the SPSS dictionary order.  It is necessary, therefore, to use the variable names as indexes or to use the range method when order matters, which is rarely.  (vardict.range() will give the entire variable list in SPSS dictionary order.)

In the first program below, the value labels for variable company are fetched but never used.

The second program won't work as written.  The valueLabels property is a dictionary where the keys are the labelled values and the values are the labels.  You would need to construct the whole value label dictionary and then assign that dictionary to the ValueLabels property.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        07/14/2011 01:48 AM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





VERY NICE Albert-Jan!!!
I didn't know one could do " varlbls[val] = v.VariableLabel"
But, unless the VECTOR variables are the first in the file they won't
correspond to the indexes in vardict.
Combining what I posted previously (without my hideous cmd crap ;-)

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
varvect=vardict.range(start="var001", end="var100")
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(varvect):
   varlbls[val] = v.VariableLabel
end program.

Question:  Will the following work as well or would we be pushing the
envelop?

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for val, v in enumerate(vardict.range(start="var001", end="var100")):
  vardict['Company'].ValueLabels [val] = v.VariableLabel
end program.
------------------------------

Albert-Jan Roskam wrote:
>
> Hoi Gerard,
>
> You mean something like this?
>
> begin program.
> """ Transfer the variable label of variableN
> to value label N of a target variable."""
> import spss, spssaux
> vardict = spssaux.VariableDict()
> vallbls = vardict['Company'].ValueLabels
> for val, v in enumerate(vardict):
>     varlbls[val] = v.VariableLabel
> end program.
>
> Cheers!!
>
> Albert-Jan
>
>
> <SNIP remaining quoted posts etc>
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-a-string-variable-tp4578333p4584712.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

 

Reply | Threaded
Open this post in threaded view
|

Re: assign variable labels to a string variable

Gerard van Meurs-2

Hello Albert-Jan (and other contributors Jon, David and Rich),

 

It works, and it is exactly what I needed. I never realised how powerful the combination of SPSS and Python can be. Thank you very much for your time and for lending your brains.

 

 

Kind regards,

 

Gerard van Meurs


Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Albert-Jan Roskam
Verzonden: vrijdag 15 juli 2011 13:56
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

 

Hoi Gerard,

Well, now I'm finally behind a computer with Python and Spss. The code below works.
vardict.range apparently returns a list, I looped over the list items, which are strings, and those strings have no ValueLabel method.

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
vallabels = dict([(n+1, vardict[v].VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])
vardict['company'].ValueLabels = vallabels
end program.

fre company.


Cheers!!
Albert-Jan

<img width=32 height=32 id="_x0000_i1025" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAccAAAEoCAIAAACijK2yAAAXSklEQVR4nO2b3ZqsuK4E6/1fmrlY09U02IltBLakiKsaFz/KMJXNXuc7nw0AAOz4zB4AACAUtCoAgCW0KgCAJbQqAIAltCoAgCW0KgCAJbQqAIAltCoAgCW0KgCAJbQqPMtnh1j8fi4u1g5ovFTxlMcSQ3Z4tuBB2ntz34znz2LxfK/ipWhVeA2eLXiQYn/df0UttmRLa9dGAjCExwse5IVW1b1Jq8L78HjBgzzdqgOLtCo8DY8XPMjNKjwf0PVaWvxMpcLT8ITBs3x2iMX2d1VxKVoVVoAnDHxwvxCpVHgHHjLwwZ1O5C0V3oTnDADAEloVAMASWhUAwBJaFQDAEloVAMASWhUAwJLy/zchAAD0UmjV2SMBAPim3KpPvhpnJ4nhMBnDBLECIQJadQ5JDIfJGCaIFQgR0KpzSGI4TMYwQaxAiIBWnUMSw2EyhglixbCQ/T87PoG+8vfbRzeUVp1DEsNhMoYJYsWYkP1ZDyltbNVHoVXnkMRwmIxhglgxIOR8SvHN8bC4/6UcfjXirP3xh9O/lxVXuPnzpFXnkMRwmIxhglhh0qrFr4odevisz2r/9nyMyds0rTqHJIbDZAwTxIp3WnXggOJdROceypRWdUwSw2EyhglihYtW/eyoHXZu1fNZvdCqc0hiOEzGMEGsMGnVrn7sPavxW/GuOgytOockhsNkDBPEijEhLUU50KqXb6ONnSs+d0GrziGJ4TAZwwSx4mbjFN9bxWuj+KzPKv4PfHFBMWE7tOockhgOkzFMECsQIqBV55DEcJiMYYJYgRABrTqHJIbDZAwTxAqECGjVOWjDn7+8OZgtLcNPCdt7o9rxxX8fvLxI8ZjaPzWuacZ8npXD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/Kp7h+/VzqZqi2I2Mfn5pndmdvpcumjVrbT754ewfTx3m4UQzaN+xls1N">

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Fri, 7/15/11, Gerard van Meurs <[hidden email]> wrote:


From: Gerard van Meurs <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Friday, July 15, 2011, 11:48 AM

Hoi Albert-Jan,

 

Thanks for your advice. Although I’m not familiar with Python, this seems to be a nice solution.

 

There is however still 1 error when I run the script. Below my (test-)script, and below that the error-message.

 

*

* read in dummy-data

*.

data list free /var1 to var10.

begin data

1 0 0 0 0 0 0 0 0 0

0 1 0 0 0 0 0 0 0 0

0 0 1 0 0 0 0 0 0 0

0 0 0 1 0 0 0 0 0 0

0 0 0 0 1 0 0 0 0 0

0 0 0 0 0 1 0 0 0 0

0 0 0 0 0 0 1 0 0 0

0 0 0 0 0 0 0 1 0 0

0 0 0 0 0 0 0 0 1 0

0 0 0 0 0 0 0 0 0 1

end data.

 

*

* label the variables

*.

variable labels

 var1 'bedrijf 1'

 var2 'bedrijf 2'

 var3 'bedrijf 3'

 var4 'bedrijf 4'

 var5 'bedrijf 5'

 var6 'bedrijf 6'

 var7 'bedrijf 7'

 var8 'bedrijf 8'

 var9 'bedrijf 9'

 var10 'bedrijf 10'.

 

*

* fill company-variable

*.

vector varlist = var1 to var10.

compute company = 0.

loop #i=1 to 10.

. do if (varlist(#i) = 1).

.  compute company = #i.

. end if.

end loop if (company = 1).

 

freq var = company.

 

*

* label the company-values with the variable labels of var1 to var10

*.

begin program.

import spss, spssaux

vardict = spssaux.VariableDict()

vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])

vardict['company'].ValueLabels = vallabels

end program.

 

freq var = company.

 

 

And this is the error message after running the script; any idea what I should change?

 

begin program.

import spss, spssaux

vardict = spssaux.VariableDict()

vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var1", end="var10"))])

vardict['company'].ValueLabels = vallabels

end program.

Traceback (most recent call last):

  File "<string>", line 4, in <module>

AttributeError: 'str' object has no attribute 'VariableLabel'

 

freq var = company.

 

 

Kind regards,

 

Gerard van Meurs


Van: SPSSX(r) Discussion [mailto:[hidden email]] Namens Albert-Jan Roskam
Verzonden: donderdag 14 juli 2011 14:17
Aan: [hidden email]
Onderwerp: Re: assign variable labels to a string variable

 

Hi Gerard, Jon,

Ok, the frist error was a typo (brain freeze after 9h of programming in R). I hope the *untested* code below tackles both this error and the 'unorderedness' dictionary problem of the previous code.

import spss, spssaux
vardict = spssaux.VariableDict()
vallabels = dict([(n+1, v.VariableLabel) for n, v in enumerate(vardict.range(start="var001", end="var100"))])
vardict['Company'].ValueLabels = vallabels

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Thu, 7/14/11, Jon K Peck <[hidden email]> wrote:


From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] assign variable labels to a string variable
To: [hidden email]
Date: Thursday, July 14, 2011, 8:53 AM

VariableDict returns a Python dictionary, so the order of the entries in it (variables) is arbitrary and will not match the SPSS dictionary order.  It is necessary, therefore, to use the variable names as indexes or to use the range method when order matters, which is rarely.  (vardict.range() will give the entire variable list in SPSS dictionary order.)

In the first program below, the value labels for variable company are fetched but never used.

The second program won't work as written.  The valueLabels property is a dictionary where the keys are the labelled values and the values are the labels.  You would need to construct the whole value label dictionary and then assign that dictionary to the ValueLabels property.

Jon Peck
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        David Marso <[hidden email]>
To:        [hidden email]
Date:        07/14/2011 01:48 AM
Subject:        Re: [SPSSX-L] assign variable labels to a string variable
Sent by:        "SPSSX(r) Discussion" <[hidden email]>





VERY NICE Albert-Jan!!!
I didn't know one could do " varlbls[val] = v.VariableLabel"
But, unless the VECTOR variables are the first in the file they won't
correspond to the indexes in vardict.
Combining what I posted previously (without my hideous cmd crap ;-)

begin program.
""" Transfer the variable label of variableN
to value label N of a target variable."""
import spss, spssaux
vardict = spssaux.VariableDict()
varvect=vardict.range(start="var001", end="var100")
vallbls = vardict['Company'].ValueLabels
for val, v in enumerate(varvect):
   varlbls[val] = v.VariableLabel
end program.

Question:  Will the following work as well or would we be pushing the
envelop?

begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for val, v in enumerate(vardict.range(start="var001", end="var100")):
  vardict['Company'].ValueLabels [val] = v.VariableLabel
end program.
------------------------------

Albert-Jan Roskam wrote:
>
> Hoi Gerard,
>
> You mean something like this?
>
> begin program.
> """ Transfer the variable label of variableN
> to value label N of a target variable."""
> import spss, spssaux
> vardict = spssaux.VariableDict()
> vallbls = vardict['Company'].ValueLabels
> for val, v in enumerate(vardict):
>     varlbls[val] = v.VariableLabel
> end program.
>
> Cheers!!
>
> Albert-Jan
>
>
> <SNIP remaining quoted posts etc>
>


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/assign-variable-labels-to-a-string-variable-tp4578333p4584712.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