BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Linlin
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
MikeZdeb
Rhodochrosite | Level 12

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

4 REPLIES 4
MikeZdeb
Rhodochrosite | Level 12

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.

Linlin
Lapis Lazuli | Level 10

Hi Mike,

Thank you very much for your explanation! - Linlin

MikeZdeb
Rhodochrosite | Level 12

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

Linlin
Lapis Lazuli | Level 10

Thank you Mike! I will read it. - Linlin

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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