%macro vartonum();
proc sql noprint;
select max(varnum) into :maxno
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='A';
QUIT;
%put &maxno;
proc sql noprint;
select name into :all_var_ separated by ' '
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='A' ;
QUIT;
%put &all_var_;
%let all_var=%sysfunc(compress(&all_var_,id));
%put &all_var;
data b; set a;run;
data E; set a;run;
%do i=1 %to &maxno;
proc sql noprint;
select name into :vnam
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='A' and varnum=&i;
QUIT;
data a_0;
set a;
if anyfirst(&vnam) ne 0 and anydigit(&vnam) ne 1 then v_id=2;
else if anyfirst(&vnam) =0 and anydigit(&vnam)=1 then v_id=1;
else v_id=0;
run;
proc sql noprint;
select avg(case when v_id=0 then . else v_id end) into :var_avg
from a_0
;quit;
%put &var_avg;
data a;
set a;
if &var_avg=1 then aa_&i.=input(&vnam,8.);
run;
data b;
set b;
if &var_avg ne 1 then aa_&i.=&vnam.;
run;
%end;
data d;
set a;
drop &all_var_;
run;
proc sql noprint;
select name into :var_t_d separated by ' '
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='D' and name ne 'id';
QUIT;
%put &var_t_d;
proc sql noprint;
select name into :var_t_a separated by ' '
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='A' and name ne 'id';
QUIT;
%put &var_t_a;
proc sql noprint;
select name into :var_t_b separated by ' '
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='A' and name ne 'id';
QUIT;
%put &var_t_b;
data c;
set b;
length missvar $30.;
array char{*} _character_;
do i=1 to dim(char);
if missing(char{i}) then call catx(' ',missvar,vname(char{i}));
end;
drop i;
if _n_=1 then call symput('missvar_',missvar);
run;
%put &missvar_;
proc sql noprint;
select name into :var_t_c separated by ' '
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='C' and name ne 'id';
QUIT;
%put &var_t_c;
data b;
set b;
drop &all_var &missvar_;
run;
proc sql noprint;
select name into :var_t_B_ separated by ' '
from sashelp.vcolumn
where LIBNAME='WORK' AND MEMNAME='B' ;
QUIT;
%put &var_t_B_;
data a;
set a;
drop &var_t_B_;
run;
proc sql;
create table union_t as
select a.*,b.* from a left join b on a.aa_1=input(compress(b.id),8.)
;quit;
data last_t;
retain &var_t_d;
set union_t;
drop &all_var id;
run;
proc sql noprint;
select compress(b.name||'='||a.name) into :renam separated by ' '
from
( select varnum,name from sashelp.vcolumn where LIBNAME='WORK' AND MEMNAME='E') a
left join
( select varnum,name from sashelp.vcolumn where LIBNAME='WORK' AND MEMNAME='D') b
on a.varnum=b.varnum
;quit;
%put &renam;
data a;
set last_t;
rename &renam;
run;
proc sql;
drop table a_0,b,c,d,e,last_t,union_t;
quit;
%mend;
%vartonum;