delete specific value label

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

delete specific value label

emma78
Hi ,
is it possible to delete a specific value label for some variables which i
put together in a macro?
I tried this but the variables need a  'to' between them, and in a macro
they are separated with a comma.



begin program.
variables = 'v1 to v5' # Specify variables here.
value = 3 # Specify value to unlabel here.
import spss,spssaux
vDict = spssaux.VariableDict(caseless = True)
varList = vDict.expand(variables)
for var in varList:
    valLabs = vDict[vDict.VariableIndex(var)].ValueLabels
    if str(value) in valLabs:
        del valLabs[str(value)]
        vDict[vDict.VariableIndex(var)].ValueLabels = valLabs
end program.


Thank you!



--
Sent from: http://spssx-discussion.1045642.n5.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: delete specific value label

Andy W
Does deleting the python object actually alter the SPSS dataset?

One approach via building syntax is to rebuild the VALUE LABEL command with
the left-over and use spss.Submit. (I don't think you can delete a value
label in SPSS syntax, but if an empty label works could do 3 ''.)



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
Sent from: http://spssx-discussion.1045642.n5.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
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: delete specific value label

emma78
Hi Andy,
yes this script deletes all value labels with code 3.



--
Sent from: http://spssx-discussion.1045642.n5.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: delete specific value label

Jon Peck
In reply to this post by emma78
When you set the value labels as in the last line of the code above, it actually generates a VALUE LABELS command that sets all the labels according to the dictionary.  I ran a similar command with and without a comma -  no problem.  If you can send an example that fails, I'll take a look.

The Dataset class modifies labels without generating syntax, but that api wasn't available when I wrote the spssaux module.

On Wed, May 19, 2021 at 7:39 AM emma78 <[hidden email]> wrote:
Hi ,
is it possible to delete a specific value label for some variables which i
put together in a macro?
I tried this but the variables need a  'to' between them, and in a macro
they are separated with a comma.



begin program.
variables = 'v1 to v5' # Specify variables here.
value = 3 # Specify value to unlabel here.
import spss,spssaux
vDict = spssaux.VariableDict(caseless = True)
varList = vDict.expand(variables)
for var in varList:
    valLabs = vDict[vDict.VariableIndex(var)].ValueLabels
    if str(value) in valLabs:
        del valLabs[str(value)]
        vDict[vDict.VariableIndex(var)].ValueLabels = valLabs
end program.


Thank you!



--
Sent from: http://spssx-discussion.1045642.n5.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


--
Jon K Peck
[hidden email]

===================== 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: delete specific value label

emma78
Hi Jon,
you're right when i did it manually it is working with the comma
But when  I use this

/begin program.
import spss, spssaux
import re

vardict = spssaux.VariableDict()
selected = []
for v in vardict:
    keys = v.ValueLabels.keys()
    values = v.ValueLabels.values()
    if keys:
        keymatch = [re.search(r"3", key, re.I) for key in keys]
        valuematch = [re.search(r"weiß+", value, re.I) for value in values]
        both = zip(keymatch, valuematch)
        test = [key is not None and val is not None for (key,val) in both]
        if any(test):
            selected.append(v.VariableName)
print selected
spss.SetMacroValue("!selectedvars36", "\n".join(selected))
end program.

begin program.
variables = '!selectedvars36' # Specify variables here.
value = 1 # Specify value to unlabel here.
import spss,spssaux
vDict = spssaux.VariableDict(caseless = True)
varList = vDict.expand(variables)
for var in varList:
    valLabs = vDict[vDict.VariableIndex(var)].ValueLabels
    if str(value) in valLabs:
        del valLabs[str(value)]
        vDict[vDict.VariableIndex(var)].ValueLabels = valLabs
end program./

i got an error message:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File
"C:\PROGRA~1\IBM\SPSS\STATIS~1\24\PYTHON\Lib\site-packages\spssaux\spssaux.py",
line 1249, in expand
    raise ValueError, _msg19 + v
ValueError: Invalid variable or TO usage: !selectedvars36



--
Sent from: http://spssx-discussion.1045642.n5.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: delete specific value label

Jon Peck
It looks like you are trying to use a macro symbol inside the Python code.  Macros can't be used in a BEGIN PROGRAM block.  They won't be expanded, because Statistics does not parse the Python code (among other reasons).

If that isn't practical, you can get around this problem by using the SPSSINC PROGRAM extension command.  You would make that BEGIN PROGRAM block into a function.

From the syntax help:

SPSSINC PROGRAM testpgm.mypgn x=age y = income z=.05.

programname specifies the Python function to be run. It has the form modulename.functionname or, if already defined in the current session, it can just be functionname. Note that modulename and functionname are case sensitive.

arguments. The argument values depend on the particular program being run. Consult the documentation for the particular program to find the specifications.

=========

Then you would put the macro reference in the SPSSINC PROGRAM line so that it would be expanded as in regular syntax.

You would use sys.argv in the begin program block to pick up the arguments from the SPSSINC PROGRAM command.  Here is an example of how to use sys.argv.  The macro would be specified on the spssinc program line/

begin program python.
def dofreq():
    import sys, spss
    for f in sys.argv[1:]
        spss.Submit("""freq %s""" % f)
end program.

spssinc program dofreq educ jobcat.


On Thu, May 20, 2021 at 1:21 AM emma78 <[hidden email]> wrote:
Hi Jon,
you're right when i did it manually it is working with the comma
But when  I use this

/begin program.
import spss, spssaux
import re

vardict = spssaux.VariableDict()
selected = []
for v in vardict:
    keys = v.ValueLabels.keys()
    values = v.ValueLabels.values()
    if keys:
        keymatch = [re.search(r"3", key, re.I) for key in keys]
        valuematch = [re.search(r"weiß+", value, re.I) for value in values]
        both = zip(keymatch, valuematch)
        test = [key is not None and val is not None for (key,val) in both]
        if any(test):
            selected.append(v.VariableName)
print selected
spss.SetMacroValue("!selectedvars36", "\n".join(selected))
end program.

begin program.
variables = '!selectedvars36' # Specify variables here.
value = 1 # Specify value to unlabel here.
import spss,spssaux
vDict = spssaux.VariableDict(caseless = True)
varList = vDict.expand(variables)
for var in varList:
    valLabs = vDict[vDict.VariableIndex(var)].ValueLabels
    if str(value) in valLabs:
        del valLabs[str(value)]
        vDict[vDict.VariableIndex(var)].ValueLabels = valLabs
end program./

i got an error message:

Traceback (most recent call last):
  File "<string>", line 6, in <module>
  File
"C:\PROGRA~1\IBM\SPSS\STATIS~1\24\PYTHON\Lib\site-packages\spssaux\spssaux.py",
line 1249, in expand
    raise ValueError, _msg19 + v
ValueError: Invalid variable or TO usage: !selectedvars36



--
Sent from: http://spssx-discussion.1045642.n5.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


--
Jon K Peck
[hidden email]

===================== 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: delete specific value label

emma78
ok, thank you, but this is far beyond my knowledge. :-( Is there another way
to delete the value labels for variables which are in a macro?



--
Sent from: http://spssx-discussion.1045642.n5.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: delete specific value label

Art Kendall
I do not fully understand what you are trying to do but these may help. (or
WHY one would do this)

It appears the difficulty is in getting the list of variables. Once you have
that segment 4 setting the labels to a blank is straightforward.

1) Select some variables into a macro.

SPSSINC SELECT VARIABLES/HELP.
Will explain how to do it.
This is an example first line
SPSSINC SELECT VARIABLES/ MACRONAME = '!InputSingle'

2) Sort some variables by some attribute in the variables view. This example
uses a custom attribute. Might be useful for setting up “TO”.

SORT VARIABLES BY ATTRIBUTE Var.Grouping.

3)this shows getting a list of variables that have a labeled value of 3. You
could cut-and-paste from the output window.

begin program python3.
import spssaux
vardict=spssaux.VariableDict()
for v in vardict:
   vls = v.ValueLabels
   if "3" in vls:
       print(v.VariableName,v.VariableLabel)
end program.

4)Once you have the list as a macro call, or a “TO” list or a cut-and-paste
string, a command similar to the following  approaches

ADD VARIABLE LABELS !SomeList
        3 “”.

ADD VARIABLE LABELS SomeVar TO SomeOtherVar
        3 “”.
ADD VARIABLE LABELS SomeVar1  SomeOtherVar AnotherVar AdditionalVar Apple
orange cherry
        3 “”.


Hope this helps.



-----
Art Kendall
Social Research Consultants
--
Sent from: http://spssx-discussion.1045642.n5.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
Art Kendall
Social Research Consultants
Reply | Threaded
Open this post in threaded view
|

Re: delete specific value label

Jon Peck
In reply to this post by emma78
In your original example, you  were deleting only one specific label but for multiple variables.  There is no way to do that in standard syntax, but you can assign a blank label to a specific value using ADD VALUE LABELS..  That will cause tables to show the value instead of a blank label.  E.g.,
VALUE LABELS x y z 3,


On Thu, May 20, 2021 at 8:36 AM emma78 <[hidden email]> wrote:
ok, thank you, but this is far beyond my knowledge. :-( Is there another way
to delete the value labels for variables which are in a macro?



--
Sent from: http://spssx-discussion.1045642.n5.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


--
Jon K Peck
[hidden email]

===================== 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: delete specific value label

Jon Peck
In reply to this post by emma78
Here is a fully worked out solution using SPSSINC PROGRAM.  First it defines the function deletelabel.  Run this once.

begin program python3.
import spssaux
vardict = spssaux.VariableDict(caseless=True)

def deletelabel():
    vars = sys.argv[1:-1]
    val = sys.argv[-1]
    for v in vars:
        vls = vardict[v].ValueLabels
        if val in vls:
            vls.pop(val)
            vardict[v].ValueLabels = vls
end program.
    
Then, in your macro or wherever you would have called the original program, run code like this.  The last item is the value whose label should be deleted delete.
    
spssinc program deletelabel jobcat minority 1.

The variable list could be a macro:
spssinc program deletelabel !victims 1.

You need to install SPSSINC PROGRAM from the Extension Hub.
This code assumes that the data are defined before running that begin program.  If not or the data might change, just move the vardict line inside the deletelabel function (indenting as the other lines are).

On Thu, May 20, 2021 at 8:36 AM emma78 <[hidden email]> wrote:
ok, thank you, but this is far beyond my knowledge. :-( Is there another way
to delete the value labels for variables which are in a macro?



--
Sent from: http://spssx-discussion.1045642.n5.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


--
Jon K Peck
[hidden email]

===================== 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