Obtain variable labels as data

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

Obtain variable labels as data

Kirill Orlov
Do you have an idea, what procedure or command in SPSS can "extract" a Variable Label and make it a data value or a data value label?
I mean - I'm interested in a way to do it *without* using OMS or Python.

Say, there is variable X with variable label 'VarX'.
I want a case to be created - in the current or in a new dataset - with either a string variable with value 'VarX' or a numeric variable having a value label 'VarX'.

[In general, SPSS ought to introduce a currently missing function VARIABLELABEL, by the analogy of function VALUELABEL.
compute strvar = VARIABLELABEL(var)
should assign a constant string value equal to the variable label of var.]

===================== 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: Obtain variable labels as data

Jon Peck
There is no way to do this without using Python or OMS (or both), but it wouldn't really make sense to write to the current dataset as the result is dimensionally inconsistent with the case data.  And since transformations are executed per case, a function such as VARIABLELABEL would be doing this for every case.  IMO it is much more useful to use Python to handle this situation, because it is not bound by the per-case behavior of transformations.  Of course, the variable label might be part of a larger expression that would vary per case, but I am having a hard time thinking of a use case for this.  APPLY DICTIONARY can copy variable labels and other metadata, but it can only transfer these to the corresponding metadata of other variables.

On Tue, Feb 2, 2021 at 7:33 AM Kirill Orlov <[hidden email]> wrote:
Do you have an idea, what procedure or command in SPSS can "extract" a Variable Label and make it a data value or a data value label?
I mean - I'm interested in a way to do it *without* using OMS or Python.

Say, there is variable X with variable label 'VarX'.
I want a case to be created - in the current or in a new dataset - with either a string variable with value 'VarX' or a numeric variable having a value label 'VarX'.

[In general, SPSS ought to introduce a currently missing function VARIABLELABEL, by the analogy of function VALUELABEL.
compute strvar = VARIABLELABEL(var)
should assign a constant string value equal to the variable label of var.]

===================== 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: Obtain variable labels as data

Kirill Orlov
Thank you very much, Jon.
I think that VARIABLELABEL function would be a great asset, though. It is very normal to assign a constant value to every case of the dataset.


02.02.2021 17:52, Jon Peck пишет:
There is no way to do this without using Python or OMS (or both), but it wouldn't really make sense to write to the current dataset as the result is dimensionally inconsistent with the case data.  And since transformations are executed per case, a function such as VARIABLELABEL would be doing this for every case.  IMO it is much more useful to use Python to handle this situation, because it is not bound by the per-case behavior of transformations.  Of course, the variable label might be part of a larger expression that would vary per case, but I am having a hard time thinking of a use case for this.  APPLY DICTIONARY can copy variable labels and other metadata, but it can only transfer these to the corresponding metadata of other variables.


===================== 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: Obtain variable labels as data

Javier Figueroa

I don't know if I'm understanding, but I think the command "VARSTOCASES" could work.
BELOW AN EXAMPLE.



/* creando base de datos ficticia.
data list list
/gender age_group motive_travel.
begin data.
1 1 7
2 2 1
1 3 2
1 4 1
2 5 1
2 2 6
end data.
execute.

numeric id(f8.0).
compute id=$casenum.
execute.
variable level all (scale).

/* asignando valores a las variables

VALUE LABELS gender
1    Male
2    Female.

VALUE LABELS age_group
1    '18-24 years'
2    '25-34 years'
3    '35-44 years'
4    '45-54 years'
5    '65-64 years'.

VALUE LABELS motive_travel
1    Ocio
2    'Visita a familiares/amigos'
3    Trabajo
4    Estudios
5    Motivos religiosos
6    Motivos de salud
7    Otros
8    Motivos no turísticos.

/* reestructurando la data para crear la variable nueva llamada VarX.
VARSTOCASES
  /ID=id1
  /MAKE Name_labels FROM gender age_group motive_travel
  /INDEX=VarX(Name_labels)
  /KEEP=id
  /NULL=KEEP.


I don't use OMS or Python.


Espero contribuir un poco.

Atentamente,



El mar, 2 feb 2021 a las 9:27, Kirill Orlov (<[hidden email]>) escribió:
Thank you very much, Jon.
I think that VARIABLELABEL function would be a great asset, though. It is very normal to assign a constant value to every case of the dataset.


02.02.2021 17:52, Jon Peck пишет:
There is no way to do this without using Python or OMS (or both), but it wouldn't really make sense to write to the current dataset as the result is dimensionally inconsistent with the case data.  And since transformations are executed per case, a function such as VARIABLELABEL would be doing this for every case.  IMO it is much more useful to use Python to handle this situation, because it is not bound by the per-case behavior of transformations.  Of course, the variable label might be part of a larger expression that would vary per case, but I am having a hard time thinking of a use case for this.  APPLY DICTIONARY can copy variable labels and other metadata, but it can only transfer these to the corresponding metadata of other variables.


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


--
Javier Figueroa
Procesamiento y Análisis de bases de datos
Cel: 5927-4748 / 4970-1940
Casa: 2289-0184

===================== 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: Obtain variable labels as data

Kirill Orlov
Javier, your VARSTOCASES yields string VarX cotaining variable *names*, not variable labels.


===================== 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: Obtain variable labels as data

Andy W
I was actually thinking current behavior of VARSTOCASES would actually be a
use case. Say for example you have a survey in wide format, and for
analysis/chart want to reshape to long format:

VARSTOCASES
  /MAKE A FROM A1 A2 A3
  /INDEX= Lab (A).

This puts in the variable Lab the values 'A1' 'A2' 'A3' etc, it does not put
their VARIABLE LABELS. (I wish it was an option to put the variable label!)

So an alternative to accomplish what I want would be (with Kirill's
hypothetical):

STRING ALab1 ALab2 ALab3 (A200).
DO REPEAT V = A1 TO A3 /L = ALab1 TO ALab3.
  COMPUTE L = VARIABLELABEL(V).
END REPEAT.

VARSTOCASES
  /MAKE A FROM A1 A2 A3
  /MAKE Lab FROM ALab1 ALab2 ALab3.

Would get me to where I want to go.




-----
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: Obtain variable labels as data

Jon Peck
The trouble with these approaches, apart from needing  hypothetical functions, is that the variable label set is dimensionally inconsistent with the case data.  But it is pretty easy to create a new dataset with the variable names and labels using OMS.

dataset declare labels.
oms select tables /if subtypes='Variable Information'
/destination format=sav outfile=labels viewer=no.
CODEBOOK all /VARINFO LABEL  /STATISTICS NONE.
omsend.

The dataset generated contains a column with variable names and a column with labels as well as some information not  needed here.

If you wanted to embed this code in a context where there might be higher up OMS commands, you could use an OMS tag to prevent OMSEND from closing those requests.



On Sat, Feb 6, 2021 at 8:45 AM Andy W <[hidden email]> wrote:
I was actually thinking current behavior of VARSTOCASES would actually be a
use case. Say for example you have a survey in wide format, and for
analysis/chart want to reshape to long format:

VARSTOCASES
  /MAKE A FROM A1 A2 A3
  /INDEX= Lab (A).

This puts in the variable Lab the values 'A1' 'A2' 'A3' etc, it does not put
their VARIABLE LABELS. (I wish it was an option to put the variable label!)

So an alternative to accomplish what I want would be (with Kirill's
hypothetical):

STRING ALab1 ALab2 ALab3 (A200).
DO REPEAT V = A1 TO A3 /L = ALab1 TO ALab3.
  COMPUTE L = VARIABLELABEL(V).
END REPEAT.

VARSTOCASES
  /MAKE A FROM A1 A2 A3
  /MAKE Lab FROM ALab1 ALab2 ALab3.

Would get me to where I want to go.




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


--
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: Obtain variable labels as data

Kirill Orlov
This is the approach I'm using in the absence of the (very convenient to become) function VARIABLELABEL.


06.02.2021 19:46, Jon Peck пишет:
The trouble with these approaches, apart from needing  hypothetical functions, is that the variable label set is dimensionally inconsistent with the case data.  But it is pretty easy to create a new dataset with the variable names and labels using OMS.

dataset declare labels.
oms select tables /if subtypes='Variable Information'
/destination format=sav outfile=labels viewer=no.
CODEBOOK all /VARINFO LABEL  /STATISTICS NONE.
omsend.

The dataset generated contains a column with variable names and a column with labels as well as some information not  needed here.

If you wanted to embed this code in a context where there might be higher up OMS commands, you could use an OMS tag to prevent OMSEND from closing those requests.



===================== 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: Obtain variable labels as data

Andy W
Here is the python approach I am doing for a project now. I stuff the old
variable labels (for everything) in a dictionary, and then after the
transform I can look up and submit ADD VALUE LABELS.

*******************************************************************.
* Getting the VARIABLE LABELS from the current data.
* and putting them into a dictionary.
BEGIN PROGRAM PYTHON3.
import spss

# Gets the variable labels in the active dataset
def get_var_labs():
    dict_labs = {spss.GetVariableName(i):spss.GetVariableLabel(i) for i in
range(spss.GetVariableCount())}
    return dict_labs

# This function grabs the SPSS data for a particular variable
# and resets value labels given a variable label dictionary
def vars_to_vals(v, var_dict):
    # Getting the SPSS data in a particular column
    allvars = [spss.GetVariableName(i) for i in
range(spss.GetVariableCount())]
    varNums = [allvars.index(i) for i in [v]]
    data = spss.Cursor(varNums)
    pydata = data.fetchall()
    data.close()
    # Create a unique set of the values
    setp = set(pydata)
    setp = [s[0] for s in setp] #each row is a tuple
    # Now adding in VALUE LABELS
    print("\nThese are the ADD VALUE LABEL commands being submitted")
    for s in setp:
        new_lab = var_dict[s]
        # Would be easier with f'' strings!
        cmd = '''ADD VALUE LABELS %(v)s \'%(s)s\' \'%(new_lab)s\'.''' % (
locals() )
        print(cmd)
        spss.Submit(cmd)

END PROGRAM.

****.
*Simple example.
DATA LIST FREE /A1 A2 A3.
BEGIN DATA
1 2 3
END DATA.
DATASET NAME Test.
VARIABLE LABELS A1 'Lab1' A2 'Lab2' A3 'Lab3'.
EXECUTE.
DISPLAY DICTIONARY.

* Gets the current variable labels.
BEGIN PROGRAM PYTHON3.
curr_vlabs =  get_var_labs()
END PROGRAM.

VARSTOCASES
  /MAKE A FROM A1 A2 A3
  /INDEX ALab (A).

* Applies the old variable labels to value labels.
BEGIN PROGRAM PYTHON3.
vars_to_vals("ALab", curr_vlabs)
END PROGRAM.

DISPLAY DICTIONARY.
****.
*******************************************************************.



-----
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: Obtain variable labels as data

Bruce Weaver
Administrator
In reply to this post by Kirill Orlov
After seeing all of the other responses, I became curious about how to do
this in Stata (the other stats package I use).  It's fairly straightforward,
as it happens.

* Generate a small dataset to illustrate.
clear
set obs 10
generate byte X = mod(_n,5)
label variable X "VarX"

* Now use the method shown in this Statalist thread:
*
https://www.statalist.org/forums/forum/general-stata-discussion/general/214574-return-variable-label
local varlabel : var label X
generate str8 NewVar = "`varlabel'"
list, clean noobs

OUTPUT from list:

    X   NewVar  
    1     VarX  
    2     VarX  
    3     VarX  
    4     VarX  
    0     VarX  
    1     VarX  
    2     VarX  
    3     VarX  
    4     VarX  
    0     VarX  




Kirill Orlov wrote

> Do you have an idea, what procedure or command in SPSS can "extract" a
> Variable Label and make it a data value or a data value label?
> I mean - I'm interested in a way to do it **without** using OMS or Python.
>
> Say, there is variable X with variable label 'VarX'.
> I want a case to be created - in the current or in a new dataset - with
> either a string variable with value 'VarX' or a numeric variable having
> a value label 'VarX'.
>
> [In general, SPSS ought to introduce a currently missing function
> VARIABLELABEL, by the analogy of function VALUELABEL.
> compute strvar = VARIABLELABEL(var)
> should assign a constant string value equal to the variable label of var.]





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

--
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
--
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: Obtain variable labels as data

Art Kendall
In reply to this post by Andy W
In a couple of weeks, I am going to have to transfer data that is now in SPSS
to an almost all-volunteer  NGO with tight budget limitations and currently
no understanding of data. They have frequent turnover due to many volunteers
only taking one unpaid gap year. So the data need an unusual degree of
documentation.

I have learned about custom attributes from Jon.  (I wish we had those back
in the 70s!).  I'll be completing adding questions, comments, and directions
to data gatherers.

Jon and Bruce have also contributed to the discussion of attributes as data.  
I'll review that when I get back to working with that data.  

However, given the renewal of the topic of attributes,  has anybody worked
out a way to take an SAV file and all in one set of syntax produce 2 new SAV
files, one with all of the data in variable view except value labels, and a
second with all of the variable names and value labels.






-----
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: Obtain variable labels as data

Jon Peck
The APPLY DICTIONARY command comes pretty close.  You would have to use it twice.

On Sun, Feb 7, 2021 at 9:12 AM Art Kendall <[hidden email]> wrote:
In a couple of weeks, I am going to have to transfer data that is now in SPSS
to an almost all-volunteer  NGO with tight budget limitations and currently
no understanding of data. They have frequent turnover due to many volunteers
only taking one unpaid gap year. So the data need an unusual degree of
documentation.

I have learned about custom attributes from Jon.  (I wish we had those back
in the 70s!).  I'll be completing adding questions, comments, and directions
to data gatherers.

Jon and Bruce have also contributed to the discussion of attributes as data. 
I'll review that when I get back to working with that data. 

However, given the renewal of the topic of attributes,  has anybody worked
out a way to take an SAV file and all in one set of syntax produce 2 new SAV
files, one with all of the data in variable view except value labels, and a
second with all of the variable names and value labels.






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


--
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: Obtain variable labels as data

Jon Peck
In reply to this post by Andy W
An alternative would be to use the spssaux.VariableDict class.  

d = spssaux.VariableDict()
creates a dictionary with all the metadata, and you can get and set it via properties.  For example,
xlabel = d]'x'].VariableLabel

On Sun, Feb 7, 2021 at 8:18 AM Andy W <[hidden email]> wrote:
Here is the python approach I am doing for a project now. I stuff the old
variable labels (for everything) in a dictionary, and then after the
transform I can look up and submit ADD VALUE LABELS.

*******************************************************************.
* Getting the VARIABLE LABELS from the current data.
* and putting them into a dictionary.
BEGIN PROGRAM PYTHON3.
import spss

# Gets the variable labels in the active dataset
def get_var_labs():
    dict_labs = {spss.GetVariableName(i):spss.GetVariableLabel(i) for i in
range(spss.GetVariableCount())}
    return dict_labs

# This function grabs the SPSS data for a particular variable
# and resets value labels given a variable label dictionary
def vars_to_vals(v, var_dict):
    # Getting the SPSS data in a particular column
    allvars = [spss.GetVariableName(i) for i in
range(spss.GetVariableCount())]
    varNums = [allvars.index(i) for i in [v]]
    data = spss.Cursor(varNums)
    pydata = data.fetchall()
    data.close()
    # Create a unique set of the values
    setp = set(pydata)
    setp = [s[0] for s in setp] #each row is a tuple
    # Now adding in VALUE LABELS
    print("\nThese are the ADD VALUE LABEL commands being submitted")
    for s in setp:
        new_lab = var_dict[s]
        # Would be easier with f'' strings!
        cmd = '''ADD VALUE LABELS %(v)s \'%(s)s\' \'%(new_lab)s\'.''' % (
locals() )
        print(cmd)
        spss.Submit(cmd)

END PROGRAM.

****.
*Simple example.
DATA LIST FREE /A1 A2 A3.
BEGIN DATA
1 2 3
END DATA.
DATASET NAME Test.
VARIABLE LABELS A1 'Lab1' A2 'Lab2' A3 'Lab3'.
EXECUTE.
DISPLAY DICTIONARY.

* Gets the current variable labels.
BEGIN PROGRAM PYTHON3.
curr_vlabs =  get_var_labs()
END PROGRAM.

VARSTOCASES
  /MAKE A FROM A1 A2 A3
  /INDEX ALab (A).

* Applies the old variable labels to value labels.
BEGIN PROGRAM PYTHON3.
vars_to_vals("ALab", curr_vlabs)
END PROGRAM.

DISPLAY DICTIONARY.
****.
*******************************************************************.



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


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