Thank you very much! - Linlin
data have;
input id v1-v5;
cards;
1 23 34 45 56 66
1 43 45 65 76 88
1 45 55 . . .
;
/* first -- this one produces the output I want */
data want;
set have;
array _v(*) v:;
array _l(5)_temporary_ ;
do _n_=1 to 5;
_l(_n_)=(lag(_v(_n_))+lag2(_v(_n_)))/2;
if _v(_n_)=. then _v(_n_)=_l(_n_);
end;
proc print;run;
/* second -- why this one doesn't work? */
data want;
set have;
array _v(*) v:;
do _n_=1 to 5;
if _v(_n_)=. then _v(_n_)=(lag(_v(_n_))+lag2(_v(_n_)))/2;
end;
proc print;run;
hi ... you have the LAG functions in a conditional statement ...
if _v(_n_)=. then _v(_n_)=(lag(_v(_n_))+lag2(_v(_n_)))/2;
so, it's not executed each time you go through the loop as it is in your first job (no conditional LAG) ...
_l(_n_)=(lag(_v(_n_))+lag2(_v(_n_)))/2;
try this ... I learned from postings by Howard Schreier that each portion of IFN is executed regardless of the "truth" of the 1st argument
data want;
set have;
array _v(*) v:;
do _n_=1 to 5;
_v(_n_) = ifn ( _v(_n_) = . , sum( lag(_v(_n_)) ,lag2(_v(_n_)) , 0)/2 , _v(_n_));
end;
run;
the SUM statement with a 0 gets rid of this message in the LOG ...
NOTE: Missing values were generated as a result of performing an operation on missing values.
hi ... you have the LAG functions in a conditional statement ...
if _v(_n_)=. then _v(_n_)=(lag(_v(_n_))+lag2(_v(_n_)))/2;
so, it's not executed each time you go through the loop as it is in your first job (no conditional LAG) ...
_l(_n_)=(lag(_v(_n_))+lag2(_v(_n_)))/2;
try this ... I learned from postings by Howard Schreier that each portion of IFN is executed regardless of the "truth" of the 1st argument
data want;
set have;
array _v(*) v:;
do _n_=1 to 5;
_v(_n_) = ifn ( _v(_n_) = . , sum( lag(_v(_n_)) ,lag2(_v(_n_)) , 0)/2 , _v(_n_));
end;
run;
the SUM statement with a 0 gets rid of this message in the LOG ...
NOTE: Missing values were generated as a result of performing an operation on missing values.
Hi Mike,
Thank you very much for your explanation! - Linlin
hi ... this is a good paper about conditional lags ... "Conditional Lags Don't Have to be Treacherous" (by Howard Schreier)
Thank you Mike! I will read it. - Linlin
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.