Help using Base SAS procedures

Lag function

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Lag function

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


Accepted Solutions
Solution
‎07-25-2013 10:02 AM
Respected Advisor
Posts: 3,156

Re: Lag function

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


All Replies
Solution
‎07-25-2013 10:02 AM
Respected Advisor
Posts: 3,156

Re: Lag function

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

New Contributor
Posts: 4

Re: Lag function

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 !

Respected Advisor
Posts: 3,156

Re: Lag function

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

New Contributor
Posts: 4

Re: Lag function

YES! That is what I want!

man, you are a genius !

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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