Изменить значение на основе «метки значения» для всех переменных - PullRequest
1 голос
/ 29 марта 2019

Я хочу обновить некоторые значения переменной на основе метки значения этой же переменной:

В псевдокоде:

for V in var1 to var10:
  if value label of V is 'x':
    set value 99

1 Ответ

1 голос
/ 29 марта 2019

Сначала мы создадим поддельные данные для демонстрации на:

data list list/a1 to a3 (3f1) notforuse (f1).
begin data 
1,2,2,55
2,1,4,66
3,4,1,77
end data.

value labels 
a1 1 'x1' 2 'x' 3 'x3'
/a2 1 'y1' 2 'a' 3 'b' 4 'x'
/a3 1 'n' 2 'x'
/notforuse 55 'nn' 66 "x".

Теперь к актуальной задаче:

* first step is to create a list of  variable labels.
dataset name origdata.
DATASET DECLARE  vallabs.
OMS   /SELECT TABLES   /IF COMMANDS=['File Information'] SUBTYPES=['Variable Values']
  /DESTINATION FORMAT=SAV  OUTFILE='vallabs' VIEWER=NO.
DISPLAY DICTIONARY.
OMSEND.
dataset activate vallabs.
* you now have a full list of your actual variable labels. next step is to select the variables you want to work on, and the labels you want to work on.

*to select the relevant variables.
select if any(var1 , "a1", "a2", "a3"). 
* can alternatively use the following: . 
select if char.index(var1, "a")>0.

*to set the new values for relevant labels:.
recode label ("x"=99)("y1"=999) into newval.
select if not missing(newval).

*now use the list to create a new syntax: .
cd 'yourpath\writeable directory'
write out='val lab syntax.sps' /"if ", var1, " = ", var2, " ", var1, " = ", newval, ".".
exe.

*now use the new syntax in the original data:.
dataset activate origdata.
insert file= 'val lab syntax.sps'.
exe.
...