BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Matt_Lin
Calcite | Level 5

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...

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

4 REPLIES 4
Haikuo
Onyx | Level 15

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

Matt_Lin
Calcite | Level 5

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 !

Haikuo
Onyx | Level 15

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

Matt_Lin
Calcite | Level 5

YES! That is what I want!

man, you are a genius !

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 4 replies
  • 1267 views
  • 4 likes
  • 2 in conversation