|
Yuan:
Four points:
--you don't need a macro for this.
--you should put macro code somewhere separate from a DATA step.
--you aren't calling the macro, just defining it. (To call it, you'd
need the statement
%count(I)
in your code.)
--you don't need the RETAIN, OUTPUT, or QUIT statements either, though
they're doing no harm in this case.
Try using an array:
DATA CHILD1; SET LTC99_1 (KEEP=SEQ REL_1-REL_20);
array rel_(20) rel_1-rel_20;
count=0;
do i=1 to 20;
if rel_(I) = 3 then count + 1;
end;
RUN;
OK?
Roger
Yuan C. wrote:
> Dear SAS-L:
> I want to count the number of appearance of 3 in each observation, and
> generate a new variable: count. But I got unexpected results using the
> following code... Can someone help on this? thanks!!
> --------------------------
> DATA:
> ------------------------
> Obs SEQ REL_1 REL_2 REL_3 REL_4 REL_5 REL_6 REL_7 REL_8
>
> 1 42 01 3 3 3 3 3
> 2 47 01 3 3
> 3 51 01 3 4
> 4 91 01 3 3
> 5 92 01 3 3 3 3
> 6 115 01 3
> 7 132 01 3
> 8 140 01 3
> .... ... ...
> ---------------------------
> My code:
> ------------------------
>
> DATA CHILD1; SET LTC99_1 (KEEP=SEQ REL_1-REL_20);
> count=0;
> do i=1 to 20;
> retain count;
> %macro count(var);
> %if rel_&var='3' %then count=count+1; %else count=count;
> %mend count;
> end;
> output;
> RUN; QUIT;
|