Существует несколько методов преобразования данных, которые вы можете попробовать.
Одним из методов является использование Proc TABULATE
для сопоставления всех значений различных переменных в набор данных, который используется для генерации имен переменных.
%macro DummyVariables(data=, var=, out=, genmode=);
proc tabulate data=&data out=class_values;
class &var;
table &var;
run;
%local dsid index seq &var _type_ varname varvalue p_type_;
%let p_type_ = 0;
%let dsid = %sysfunc(open(class_values));
%syscall SET ( dsid );
data &out;
set &data;
length
%if &genmode=1 %then %do;
%do index = 1 %to %sysfunc(ATTRN(&dsid,NOBS));
dummy_&index
%end;
%end;
%if &genmode=2 %then %do;
%do %while (0 = %sysfunc(fetch(&dsid)));
%let index = %sysfunc(index(&_type_, 1));
%let varname = %scan(&var,&index);
%if &_type_ ne &p_type_ %then %let seq=1; %else %let seq=%eval(&seq+1);
&varname._&seq
%let p_type_ = &_type_;
%end;
%end;
%if &genmode=3 %then %do;
%do %while (0 = %sysfunc(fetch(&dsid)));
%let index = %sysfunc(index(&_type_, 1));
%let varname = %scan(&var,&index);
%let varvalue = &&&varname;
"&varname._&varvalue"n
%end;
%end;
4;
run;
%let dsid = %sysfunc(close(&dsid));
%mend;
%DummyVariables (data=sashelp.class, var=name age sex height weight, out=want1, genmode=1);
%DummyVariables (data=sashelp.class, var=name age sex height weight, out=want2, genmode=2);
options validvarname = any;
%DummyVariables (data=sashelp.class, var=name age sex height weight, out=want3, genmode=3);
режим 1 одна фиктивная переменная с числовым суффиксом для каждой отдельной переменной и значения;
NOTE: Variable dummy_1 is uninitialized.
NOTE: Variable dummy_2 is uninitialized.
...
NOTE: Variable dummy_58 is uninitialized.
NOTE: Variable dummy_59 is uninitialized.
mode 2 одна фиктивная переменная с суффиксом с числовым индексом для каждой отдельной переменной AND. Именные имена переменных основаны на исходных именах переменных.
NOTE: Variable name_1 is uninitialized.
...
NOTE: Variable name_19 is uninitialized.
NOTE: Variable age_1 is uninitialized.
...
NOTE: Variable age_6 is uninitialized.
NOTE: Variable sex_1 is uninitialized.
NOTE: Variable sex_2 is uninitialized.
NOTE: Variable height_1 is uninitialized.
...
NOTE: Variable height_17 is uninitialized.
NOTE: Variable weight_1 is uninitialized.
etc ...
mode 3 фиктивная переменная с суффиксом одного значения для каждой отдельной переменной AND. Именные имена переменных на основе имен переменных И значений.
NOTE: Variable name_Alfred is uninitialized.
...
NOTE: Variable name_William is uninitialized.
NOTE: Variable age_11 is uninitialized.
...
NOTE: Variable age_15 is uninitialized.
NOTE: Variable age_16 is uninitialized.
NOTE: Variable sex_F is uninitialized.
NOTE: Variable sex_M is uninitialized.
NOTE: Variable 'height_51.3'n is uninitialized.
NOTE: Variable 'height_56.3'n is uninitialized.
...
NOTE: Variable height_69 is uninitialized.
NOTE: Variable height_72 is uninitialized.
NOTE: Variable 'weight_50.5'n is uninitialized.
NOTE: Variable weight_77 is uninitialized.
...
NOTE: Variable weight_133 is uninitialized.
NOTE: Variable weight_150 is uninitialized.