Hi All,
I just realized that, if there is some condition before using LAG the function, then LAG will take the "previous one" from those that satisfy the condition ?
For example,
n A Name C
1 0 Abbie
2 1 Bob
3 . Charlie
4 0 David
5 1 Eddie
6 . Frank
If A = . then C = lag(B);
for record # 6, C will have Charlie, not Eddie.
How do I get Eddie ??
Many Thanks for quick response, working in the office now...
When using lag() /dif() conditionally, you need to be aware of what is happening, in your case, you need using it unconditionally:
c=ifc(missing(a), lag(b), c);
In this case, lag() will be processed for every row of record, another option is to create a new variable to address the lag() scenario, then drop it.
Please read SAS help Docs on lag() for deeper understanding.
Haikuo
When using lag() /dif() conditionally, you need to be aware of what is happening, in your case, you need using it unconditionally:
c=ifc(missing(a), lag(b), c);
In this case, lag() will be processed for every row of record, another option is to create a new variable to address the lag() scenario, then drop it.
Please read SAS help Docs on lag() for deeper understanding.
Haikuo
Thanks! I understand.
But there is more to the question when more than 1 row is missing consecutively: (and number of missing rows is indefinite)
n A Name C (intended answer) C(currently achieved using either method)
1 1 Abbie Abbie Abbie
2 . Bob Abbie Abbie
3 . Charlie Abbie .
4 1 David David David
5 . Eddie David David
6 . Frank David .
7 . George David .
If I kept running SAS code your suggested manually, it will work out "eventually" (3 David -> 4 David -> etc...), but that won't be a solution.
Many many thanks !
Well, in that case, you would need something a little different:
data havE;
input n A Name $;
cards;
1 1 Abbie
2 . Bob
3 . Charlie
4 1 David
5 . Eddie
6 . Frank
7 . George
;
data want;
set have;
length c $ 8;
retain c;
if not missing(a) then c=name;
run;
Haikuo
YES! That is what I want!
man, you are a genius !
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 the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.