create several variables in do loop by concatenating already existing
create several variables in do loop by concatenating already existing
(OP)
Hello,
The problem I have is the following: In my program, I want to create a new variable when a current variable has a missing value. This is what I already have:
data basetabel_imputed;
set basetablenum;
array wit{*} _numeric_;
do i=1 to dim(wit);
if wit(i)=. then do;
wit(i)= symget(vname(wit(i));
end;
end;
drop i;
run;
I read in the variables, make an array of the numeric ones and then, for every numeric variable, I change the missing value by the median of that variable. Now, in the second do loop, I want to create a new variable, that has the same name as the variable currently accessed by the do loop, except that I want to add the suffix mv to it. Then I want this new variable to take the value 1. What I want is in fact a statement that does:
vname(wit(i))||mv = 1;
(but his doensn't work because one cannot create a new variable name like that).
Matthijs
The problem I have is the following: In my program, I want to create a new variable when a current variable has a missing value. This is what I already have:
data basetabel_imputed;
set basetablenum;
array wit{*} _numeric_;
do i=1 to dim(wit);
if wit(i)=. then do;
wit(i)= symget(vname(wit(i));
end;
end;
drop i;
run;
I read in the variables, make an array of the numeric ones and then, for every numeric variable, I change the missing value by the median of that variable. Now, in the second do loop, I want to create a new variable, that has the same name as the variable currently accessed by the do loop, except that I want to add the suffix mv to it. Then I want this new variable to take the value 1. What I want is in fact a statement that does:
vname(wit(i))||mv = 1;
(but his doensn't work because one cannot create a new variable name like that).
Matthijs
RE: create several variables in do loop by concatenating already existing
data basetabel_imputed (drop=varname val row n) varnames (keep= varname val row);
set basetabel (obs=50 ) end=last;
array wit{*} _numeric_;
n + 1;
do i=1 to dim(wit);
if wit(i)=. then do;
varname = 'mv'||vname(wit(i));
val = 1;
row = n;
output varnames;
end;
end;
*output basetabel_imputed;
if last then do;
call execute('proc transpose data=varnames out=varnames2 (keep=mv:);var val;id varname;by row;run;');
do i=1 to n;
set basetabel point=i;
set varnames2 point=i;
output basetabel_imputed;
end;
end;
drop i;
run;
BIGuidance
www.biguidance.com