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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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