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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

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
  • 1007 views
  • 4 likes
  • 2 in conversation