0
votes

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.

1
Variable names generally don't start with a digit. If they do you need to use a name literal syntax. Are the names actually the variable labels ? Can you show the Proc CONTENTS output for the data set ? Can you show some specific example code of what you want instead just describing the general case.Richard

1 Answers

1
votes

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.