SPSS – extract value labels

Task:

  • You need change value labels of variable values in the data file. It is enough to change some texts regurarly and you don’t need to prepare whole new value labels syntax.
  • Here is to use the rational syntaxe, which extract actual value labels to the XLS file. You have to chage the texts only and than transfer the texts to the value label syntax.

How it works:

  • You have to change the path to the source SPSS data file at the beginning of the syntax and change the path to the output XLS file at the end of the syntax zdrojovému SPSS souboru a také na konci syntaxe upravit cestu k výstupnímu XLS souboru.
  • This syntax excludes all of the string variables and also all of the numeric variables without Value Labels.
  • Syntax cumulates variables with the same value labels.
  • And than it extraxcts value labels into the structured XLS file.
  • You can work with extracted data and prepare final SPSS data file. You can also use our XLS application ”Data manager” and use the automation os the data files.

Do you need something different?

  • If you use specific data managers and you want change structure of the final extraction, don’t hesitate to contact us in the section ”contact us” and we are able to change the final structure of the XLS file according to your requirements.

Here is the syntax for extraction of value labels from SPSS data file.

  dataset close all.
  get file = "...path...\NAME_data_00.sav".
  dataset name data.

  numeric @var_start @var_end (f1).
  match files
    /file = *
    /keep = @var_start all.
  execute.
  sort variables by type.
  execute.
  match files
    /file = *
    /keep = @var_start to @var_end.
  execute.
  delete variables @var_start @var_end.
  add value labels all -937951463476 "@@value_label@@delete@@".

  output new name = labels.
  output activate labels.

  dataset declare data_labels.
  oms /select all /exceptif subtypes = ['Notes' 'Titl*' 'Warn*']
    /destination format = sav outfile = data_labels.
  codebook all /varinfo valuelabels /statistics none.
  omsend.

  output close labels.
  dataset activate data_labels.

  select if (Value <> "@@value_label@@delete@@").
  execute.

  rename variables (Label_ = var)(Value = lab).
  numeric val id (f12).
  string vars labels var_val (a10000).
  alter type Var2 (f12).
  rename variables (Var2 = val).
  compute id = $casenum.
  execute.
  delete variables Command_ Subtype_ Var1.

  do if (id=1 or var<>lag(var)).
    compute labels = concat(ltrim(rtrim(string(val,f12))),"-",ltrim(rtrim(lab))).
  else.
    compute labels = concat(ltrim(rtrim(lag(labels))),"|",ltrim(rtrim(string(val,f12))),"-",ltrim(rtrim(lab))).
  end if.
  execute.

  aggregate /outfile = * mode=addvariables overwritevars=yes /break = var /labels=last(labels).

  do if (char.rindex(var,"_")>0).
    compute vars = char.substr(var,1,char.rindex(var,"_")-1).
  else.
    compute vars = ltrim(rtrim(var)).
  end if.
  execute.

  sort cases by vars (a).
  if (vars=lag(vars)) id = lag(id).
  execute.
  sort cases by id (a).

  do if ($casenum=1).
    compute id = 1.
  else if (labels<>lag(labels)).
    compute id = lag(id) + 1.
  else.
    compute id = lag(id).
  end if.
  execute.

  dataset activate data_labels.
  dataset copy data_labels.
  dataset name data_labels_vars.
  dataset activate data_labels.

  sort cases id val (a).
  compute id_pom=0.
  do if ($casenum=1 or val<>lag(val) or id<>lag(id)).
    compute id_pom = 1.
  end if.
  select if (id_pom=1).
  compute var_val = ltrim(rtrim(string(val,f12))).
  execute.

  delete variables var val vars labels id_pom.

  dataset activate data_labels_vars.
  sort cases id (a).
  compute id_pom=0.
  do if ($casenum=1 or var<>lag(var) or id<>lag(id)).
    compute id_pom = 1.
  end if.
  select if (id_pom=1).
  execute.

  do if ($casenum=1 or vars<>lag(vars) or labels<>lag(labels)).
    compute var_val = ltrim(rtrim(var)).
  else.
    compute var_val = concat(ltrim(rtrim(lag(var_val)))," ",ltrim(rtrim(var))).
  end if.
  execute.

  aggregate /outfile = * mode=addvariables overwritevars=yes /break = vars labels /var_val=last(var_val).

  compute id_pom=0.
  do if ($casenum=1 or var_val<>lag(var_val)).
    compute id_pom = 1.
  end if.
  select if (id_pom=1).
  execute.

  delete variables var val lab vars labels id_pom.

  add files
   /file = data_labels_vars
   /file = data_labels
  .
  execute.
  dataset name data_labels_final.
  dataset activate data_labels_final.

  sort cases id (a).

  save translate outfile = "...path...\NAME_data_00_value_labels.xlsx"
   /type = xls /version = 12 /replace
   /keep = var_val lab.

  dataset activate data.
  dataset close data_labels.
  dataset close data_labels_vars.
  dataset close data_labels_final.