Consider
data want;set have;array ay1{7};retain ay1:;if first.tcol then do;call missing(of ay1{*});end;if ay1{7} > a then do;[other statements to determine value of ay1]end;else do;[other statements to determine value of ay1]end;by tcol;run;
Since array ay1
will be automatically retained between observations, if I want the program to do some by-group processing, I need to reset ay1
values when it encounter a new tcol
value (otherwise it will inherit values from last observation of previous tcol
value. This will affect if ay{7} > a
, hence all other statements followed will by affected).
In my current codes, I will reset the array by
do i = 1 to dim(ay1);ay1{i} = 0;end;
And this work fines. For the first obs of each tcol
value, It will first reset all values of ay1
to 0 and then perform [other statement]
to update ay1
in this observation.
If I use call missing(of ay1{*});
, for the first obs of each tcol
value, it will set each value of ay1
to missing (as expected). But the following [other statement]
will NOT update ay1
. (I have placed several put statements in [other statement]
as debug step to make sure this part has run. It does all other work except updating ay1
value).
If first.tcol
fails, everything seems back to normal (no error but the output data set is wrong since first step in each by group has all unexpected values). So I think there must be something wrong about using call missing
here.
Best Answer
Your statement "Since array ay1 will be automatically retained between observations" is incorrect. Arrays declared as _TEMPORARY_
are retained automatically. Arrays of permanent variables are not.
You can test this with:
data want;set have;array ay1{7};by tcol;if first.tcol then do;do i=1 to 7;ay1[i] = 1;end;end;run;
You will see that after the first tcol value in each group, the values will be missing. I.E. they were not retained between rows.
Add
retain ay:;
to your Data Step and it should work as you expect.
EDIT: Added this to show it works as described.
data have;tcol = 1;a = 1;output;tcol = 1;a = 10;output;run;data want;set have;array ay1{7};retain ay1:;by tcol;if first.tcol then do;call missing(of ay1{*});end;if ay1{7} > a then do;do i=1 to 7;ay1[i] = -a;end;end;else do;do i=1 to 7;ay1[i] = 999;end;end;run;