|
"Yuan C." <peppermint_jojo@YAHOO.COM> wrote in message
news:200401240453.i0O4rAS29993@listserv.cc.uga.edu...
> Dear SAS-l;
> My variable names in an "IF" condition is similar, is there a way that
I
> can use macro or do-loop to do it? thanks!
>
> Yuan
>
> --------------------------------------------
>
> IF ICL_ST01^=' ' THEN N1=1; ELSE N1=0;
> IF ICL_ST02^=' ' THEN N2=1; ELSE N2=0;
> ... ...
> (up to ICL_ST20)
> ... ...
> IF ICL_ST20^=' ' THEN N20=1; ELSE N20=0;
> N=SUM(N1,N2,...N20);
>
> ----------------------------------------------
Short: No
Medium: Maybe
Long: I dunno
Should or should not is not really a question I can answer. Depends on the
size of the Data Set, and how affected you are by the actual run time or cpu
time. In your case I recommend "use the code that most clearly indicates
what the data step is doing" (your dialect of clarity might differ from
mine:-)
One small problem with macro is that there is no allowance for open-code %if
or %do, which means you have to define a macro and invoke.
-----
no macro
It's a little tricky that your varnames are z2. suffixed, but variable list
syntax is up to the task.
N = n(of icl_st01-icl_st20);
or
array check icl_st01-icl_st20;
N = 0;
do i = lbound(check) to hbound(check);
n + check[i]^='';
end;
-----
macro
%macro check(array=, lower=,upper=, fmt=);
%* no checks on properness of parameter values;
%local i ifmt comma;
%do i = &lower %to &upper;
%let ifmt = %sysfunc(putn(&i,&fmt));
%* emit data step source text;
if &array&ifmt ^= "" then n&i=1; else n&i=0;
%end;
n = sum(N&lower
%do i = %eval(&lower+1) %to &upper;
, N&i
%end;
);
%mend;
data ...
...
%check (array=icl_st,lower=1, upper=20, fmt=z2.)
...
run;
other macros could emit other data step code that perform the same
computation in other ways.
Which should you use ?
That which is most fast ? That which is most clear ?
--
Richard A. DeVenezia
http://www.devenezia.com/downloads/sas/macros/
|