DATA Step, Macro, Functions and more

Why my second code doesn't work?

Accepted Solution Solved
Reply
Super Contributor
Posts: 1,636
Accepted Solution

Why my second code doesn't work?

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;


Accepted Solutions
Solution
‎05-08-2012 10:10 PM
Valued Guide
Posts: 765

Re: Why my second code doesn't work?

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.

View solution in original post


All Replies
Solution
‎05-08-2012 10:10 PM
Valued Guide
Posts: 765

Re: Why my second code doesn't work?

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.

Super Contributor
Posts: 1,636

Re: Why my second code doesn't work?

Hi Mike,

Thank you very much for your explanation! - Linlin

Valued Guide
Posts: 765

Re: Why my second code doesn't work?

hi ... this is a good paper about conditional lags ... "Conditional Lags Don't Have to be Treacherous" (by Howard Schreier)

http://www.howles.com/saspapers/CC33.pdf

Super Contributor
Posts: 1,636

Re: Why my second code doesn't work?

Thank you Mike! I will read it. - Linlin

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 297 views
  • 3 likes
  • 2 in conversation