Reputation: 19
I have a few columns in SAS for which name starts with 100_Section_xxx
. So first part of the name is the same, while second part (xxx
) is different.
I want to write condition for every column such as If 100_Section_xxx >1 then do error_100_Section_xxx ="yes"
How do I write it, in order Sas takes the second part of the name of 100_Section_xxx
and add xxx
to the second part of the name of the column error_100_Section_xxx
.
Upvotes: 0
Views: 235
Reputation: 12465
You can do this with a simple macro and data step with an array:
This macro loops through a list of names (the XXX values) and lists variables that end with it.
%macro var_names(names, prefix=);
%local i n var;
%let n=%sysfunc(countw(&names));
%do i=1 to &n;
%let var=%scan(&names,&i);
&prefix.&var
%end;
%mend;
Now use that, a data step, and 2 arrays.
data have;
_100_Section_a = 1;
_100_Section_b = 0;
_100_Section_c = 10;
run;
data want;
set have;
array vars[*] %var_names(a b c, prefix=_100_Section_);
format %var_names(a b c, prefix=error_100_Section_) $8.;
array errors[*] %var_names(a b c, prefix=error_100_Section_);
do i=1 to dim(vars);
if vars[i] > 1 then
errors[i] = "yes";
end;
drop i;
run;
Note, I added a _
to the variable names. SAS variables cannot normally start with a number.
Upvotes: 1