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 |
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 |
The file of one of my
customers is organized as follows: - company1 ‘label
for company - company2 ‘label
for company - - company100 ‘label
for company 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 Van: SPSSX(r)
Discussion [mailto:[hidden email]] Namens
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?
|
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.
--
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/). |
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
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?" |
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 |
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 ;-) ---
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?" |
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 > 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 |
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 > 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 >> 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 |
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. ----
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?" |
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 |
|
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. ------------------------------
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?" |
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 |
|
In reply to this post by Jon K Peck
|
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 AttributeError: 'str' object has no attribute 'VariableLabel' freq var = company. Kind regards, Van: SPSSX(r)
Discussion [mailto:[hidden email]] Namens
Albert-Jan Roskam
|
|
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, Van: SPSSX(r)
Discussion [mailto:[hidden email]] Namens
Albert-Jan Roskam
|
Free forum by Nabble | Edit this page |