Hello,
I hope you can help me:-) Is there a possibility to access value labels with python? I have a lot of variables and want to get all variables which have the value label 'completely disagree' or -that would be even better ;-)-get all variables which value labels have only 1 digit codes (codes 1-9). After that i would like to du further transformations with those variables. Thans for your help! |
Sure, Python has full access to all the
dictionary properties. Below is a simple program that prints the
names of all variables that have value labels with the maximum length of
the labelled values < 2.
This constructs a list, which can be used in many ways, so more information is needed to know where to go next. The code goes step by step, printing intermediate results, to make it easier to understand. begin program. import spss, spssaux vardict = spssaux.VariableDict() onedigits = [] for v in vardict: vls = v.ValueLabels.keys() print vls if vls: lengths = [len(val) for val in vls] if max(lengths) < 2 : onedigits.append(v.VariableName) print onedigits end program. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: emma78 <[hidden email]> To: [hidden email] Date: 01/31/2015 04:31 PM Subject: [SPSSX-L] access value labels with python Sent by: "SPSSX(r) Discussion" <[hidden email]> Hello, I hope you can help me:-) Is there a possibility to access value labels with python? I have a lot of variables and want to get all variables which have the value label 'completely disagree' or -that would be even better ;-)-get all variables which value labels have only 1 digit codes (codes 1-9). After that i would like to du further transformations with those variables. Thans for your help! -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/access-value-labels-with-python-tp5728541.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 emma78
Hi Jon,
thanks so much fo your quick reply! Just a Little question: if max(lengths) =2 is not possible? What I want to do next, is to adjust the length of the variable according to the length of the max value in the value label. For example I have v1 Gender original width=4, only one Digit is needed v2 codes 1-99, original width=4, so the width should be 2 In SPSS I use alter type for this Problem, but I have to look at all variables which max length I have to write in the Syntax, and thats quite a lot work;-) Thank you! |
Use == for the equality test, e.g.,
if max(lengths) == 2 : Once you have selected the variables, you might want to generate a macro listing them all. In the code I posted, you could add at the end spss.SetMacroValue("!thevars", " ".join(onedigits)) and then use that macro - !thevars - in an ALTER TYPE command. But I'm puzzled why you would want to do this. For numeric values, everything is stored as a 64-bit, double precision floating point number, so you can't alter the widths. For strings, you can set the widths, but internally a string will always occupy at least eight bytes anyway. You might want to download the Programming and Data Management book from here https://www.ibm.com/developerworks/mydeveloperworks/files/form/anonymous/api/library/b5bb8a42-04d2-4503-93bb-dc45d7a145c2/document/ea95a2e5-88f9-404c-86ef-13f19951bd74/media/Programming%20and%20Data%20Management%20for%20IBM%20SPSS%20Statistics%2020.zip for more examples of using Python to do typical Statistics tasks (among other things). spss.SetMacroValue( Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: emma78 <[hidden email]> To: [hidden email] Date: 02/01/2015 07:54 AM Subject: Re: [SPSSX-L] access value labels with python Sent by: "SPSSX(r) Discussion" <[hidden email]> Hi Jon, thanks so much fo your quick reply! Just a Little question: /if max(lengths) =2/ is not possible? What I want to do next, is to adjust the length of the variable according to the length of the max value in the value label. For example I have v1 Gender original width=4, only one Digit is needed v2 codes 1-99, original width=4, so the width should be 2 In SPSS I use alter type for this Problem, but I have to look at all variables which max length I have to write in the Syntax, and thats quite a lot work;-) Thank you! -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/access-value-labels-with-python-tp5728541p5728543.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 emma78
I have to transform the data in a triple S data set and therefore has to adjust the width to the Maximum value.
I can use alter type ALL (A = A100). for strings to get them all to a width of 100 for example, or use Amin. But I don`t know how do to that for numeric variables automatically. Is there is a smarter way to do that? |
One last question:
Is there a Limitation in the script you posted? I have just tried it and it stopped after the first 250 variables? Thank you! |
In reply to this post by emma78
Are you trying to read a triple-s dataset?
If so, try the STATS GET TRIPLES extension command. You can
install it via Utilities > Extension Bundles > Download and Install
Extension Bundles if you are on Statistics 22 or Get it from the SPSS Community
website for earlier versions from here
https://www.ibm.com/developerworks/community/files/app?lang=en#/file/b72f7f82-5f77-4693-8865-3660764a4972 But I'm still puzzled about what you need to do. Numeric variables may have a format width (default F8.2), but their storage size is fixed. Reading data might involve a different format, but you could never hold 100 digits internally in a numeric variable. Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: emma78 <[hidden email]> To: [hidden email] Date: 02/01/2015 09:33 AM Subject: Re: [SPSSX-L] access value labels with python Sent by: "SPSSX(r) Discussion" <[hidden email]> I have to transform the data in a triple S data set and therefore has to adjust the width to the Maximum value. I can use alter type ALL (A = A100). for strings to get them all to a width of 100 for example, or use Amin. But I don`t know how do to that for numeric variables automatically. Is there is a smarter way to do that? -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/access-value-labels-with-python-tp5728541p5728545.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 |
No I want to save a SPSS File in triple s or ascii. But I have to do some transformations first so that there are no unnecessary blanks in my dataset,
for example for v1 gender: In the original file it looks like Startposition:___1 or Startposition:___2 (_ stand for the blanks, 1:male, 2:female) But it should look like (because we need only one digit) Startposition:1 Startposition:2 Is there a limitation? Because the script stops after 250 variables. |
In reply to this post by emma78
Oh - I take it that you want to set the Print Formats to the minimum,
=====================
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
according to the labels. That should be simple with Python, adapting the other code. But I think that a simple Print would insert a blank between variables. Write? One old way, easy for a one-time task, would be to write out a comma-delimited file, and then use a simple text editor to remove all the commas. -- Rich Ulrich > Date: Sun, 1 Feb 2015 07:52:17 -0700 > From: [hidden email] > Subject: Re: access value labels with python > To: [hidden email] > > Hi Jon, > thanks so much fo your quick reply! > > Just a Little question: /if max(lengths) =2/ is not possible? > > What I want to do next, is to adjust the length of the variable according to > the length of the max value in the value label. > > For example I have > v1 Gender original width=4, only one Digit is needed > v2 codes 1-99, original width=4, so the width should be 2 > > In SPSS I use alter type for this Problem, but I have to look at all > variables which max length I have to write in the Syntax, and thats quite a > lot work;-) > > Thank you! > > |
Free forum by Nabble | Edit this page |