number of value labels in a variable: Python

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

number of value labels in a variable: Python

drfg2008
how can I compute the number of labels of a variable? For example I have 351 labels in a variable, that might be 349 in one file and 352 in another.

I heard of a possibility in Python, but tried unsucessfully the following:


BEGIN PROGRAM.
import spss

FileN=spss.GetVariableCount()
i = 0
array=[]
while i < FileN:
         label = spss.GetVariableLabel(i)
         array.append(label)
         i+=1
print array

number_label_variable= array.count('variable')
print("Number of value labels: ")
print number_label_variable

END PROGRAM.


Thank you for some help.
Frank
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: number of value labels in a variable: Python

jkpeck
The title refers to value labels, but the code refers to variable labels.

If you are just looking for differences in properties between files, you can use the compare datasets extension command selecting the attributes and/or data files to compare.

If you really need to count value labels for a variable, you could do like this
begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
for v in vardict:
  lblcount = len(v.ValueLabels)
  print v.VariableName, lblcount
end program.
Reply | Threaded
Open this post in threaded view
|

Re: number of value labels in a variable: Python

drfg2008
My description was a bit confusing: I meant the number of value labels (i.e. "2" for male/female for the variable gender).

Thanks for the code and ideas (private message)!

However, one more question. I'm interested in the number of value labels of a certain variable (not all variables). I chaged the code (see below). Is there a faster (or more elegant) solution to it?

My version:

BEGIN PROGRAM.
import spss, spssaux
vardict = spssaux.VariableDict()
for v in vardict:
  lblcount = len(v.ValueLabels)
  if v == "SICYear" :
          anzahl=len(v.ValueLabels)
          print anzahl
 end program.


Frank
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: number of value labels in a variable: Python

Garry Gelade
Frank,

You can pass an argument to VariableDict() eg

vardict = spssaux.VariableDict(['SICYear'])

So you can write:


BEGIN PROGRAM.
import spss, spssaux
print len(spssaux.VariableDict(['SICYear'])[0].ValueLabels)
END PROGRAM.

Not sure whether it is fast or elegant. It is geeky though.


Garry Gelade
Business Analytic Ltd

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
drfg2008
Sent: 03 June 2011 07:32
To: [hidden email]
Subject: Re: number of value labels in a variable: Python

My description was a bit confusing: I meant the number of value labels (i.e.
"2" for male/female for the variable gender).

Thanks for the code and ideas (private message)!

However, one more question. I'm interested in the number of value labels of
a certain variable (not all variables). I chaged the code (see below). Is
there a faster (or more elegant) solution to it?

My version:

BEGIN PROGRAM.
import spss, spssaux
vardict = spssaux.VariableDict()
for v in vardict:
  lblcount = len(v.ValueLabels)
  if v == "SICYear" :
          anzahl=len(v.ValueLabels)
          print anzahl
 end program.


Frank

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/number-of-value-labels-in-a-va
riable-Python-tp4449235p4450299.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: number of value labels in a variable: Python

drfg2008
yes, it seems to be a bit faster,

thanks !

Frank
Dr. Frank Gaeth

Reply | Threaded
Open this post in threaded view
|

Re: number of value labels in a variable: Python

Jon K Peck
In reply to this post by drfg2008
This will do it.

begin program.
import spss, spssaux
lblcount = len(spssaux.VariableDict("jobcat")[0].ValueLabels)
print lblcount
end program.



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




From:        drfg2008 <[hidden email]>
To:        [hidden email]
Date:        06/03/2011 12:34 AM
Subject:        Re: [SPSSX-L] number of value labels in a variable: Python
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




My description was a bit confusing: I meant the number of value labels (i.e.
"2" for male/female for the variable gender).

Thanks for the code and ideas (private message)!

However, one more question. I'm interested in the number of value labels of
a certain variable (not all variables). I chaged the code (see below). Is
there a faster (or more elegant) solution to it?

My version:

BEGIN PROGRAM.
import spss, spssaux
vardict = spssaux.VariableDict()
for v in vardict:
 lblcount = len(v.ValueLabels)
 if v == "SICYear" :
         anzahl=len(v.ValueLabels)
         print anzahl
end program.


Frank

-----
Dr. Frank Gaeth
FU-Berlin

--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/number-of-value-labels-in-a-variable-Python-tp4449235p4450299.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