+ Reply to Thread
Results 1 to 4 of 4

Thread: Lag function in SAS not updating to reflect new values.

  1. #1
    Devorador de queso
    Points: 90,481, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent Poster
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,844
    Thanks
    305
    Thanked 2,605 Times in 2,222 Posts

    Lag function in SAS not updating to reflect new values.




    I have some SAS code that needs to reference some values in a previous row. The lag function is typically how one deals with that. The issue is that the value that I grab using lag then (possibly) gets changed and the change isn't reflected in the next row. I've provided a short example but basically think of 'x' as the starting value and 'y' as the ending value and I don't want any of the intervals to be overlapping so if the starting value is <= the previous ending value I want to shift the interval over and keep repeating this process.
    Code: 
    data test;
    	input x y;
    	datalines;
    1 2
    1 4
    5 7
    ;
    
    data new;
    	set test;
    	diff = y - x;
    	lg = lag(y);
    	if x < lg then x = lg + 1;
    	* update x and y to be shifted;
    	y = x + diff;
    run;
    My output gives:
    Code: 
                                         Obs    x    y    diff    lg
    
                                          1     1    2      1      .
                                          2     3    6      3      2
                                          3     5    7      2      4
    I would have expected/desired

    Code: 
                                         Obs    x    y    diff    lg
    
                                          1     1    2      1      .
                                          2     3    6      3      2
                                          3     7    9      2      6
    So basically in the final row the lagged value for y is reading the original unmodified value of 4 instead of the updated value of 6. I think this is due to lag being implemented as a queue but I don't know how to tell lag to update after I change the value. I'm open to any suggestions - I'm not married to using lag here if there is another way around the issue.
    I don't have emotions and sometimes that makes me very sad.

  2. #2
    Omega Contributor
    Points: 35,945, Level: 100
    Level completed: 0%, Points required for next Level: 0
    hlsmith's Avatar
    Location
    Not Ames, IA
    Posts
    6,750
    Thanks
    383
    Thanked 1,137 Times in 1,098 Posts

    Re: Lag function in SAS not updating to reflect new values.

    Do you really want to be over-writing values instead of creating new values to use within data steps?


    That always seems dangerous, like calling a now dataframe the same thing as the augmented prior dataframe.
    Stop cowardice, ban guns!

  3. #3
    Devorador de queso
    Points: 90,481, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent Poster
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,844
    Thanks
    305
    Thanked 2,605 Times in 2,222 Posts

    Re: Lag function in SAS not updating to reflect new values.

    If I have to make new columns that contain the desired output that is fine. So if there ends up being 'newx' and 'newy' columns that give me what I want I'd be fine with that.
    I don't have emotions and sometimes that makes me very sad.

  4. #4
    Devorador de queso
    Points: 90,481, Level: 100
    Level completed: 0%, Points required for next Level: 0
    Awards:
    Posting AwardCommunity AwardDiscussion EnderFrequent Poster
    Dason's Avatar
    Location
    Tampa, FL
    Posts
    12,844
    Thanks
    305
    Thanked 2,605 Times in 2,222 Posts

    Re: Lag function in SAS not updating to reflect new values.


    Was able to make it work using retain.

    Code: 
    
    data attempt2;
    	set initial;
    	diff = y - x;
    	if x <= oldy then x = oldy + 1;
    	y = x + diff;
    	oldy = y;
    	retain oldy;
    run;
    So the first time through when it is first referenced oldy will be missing since it hasn't been initialized. That is fine for what I'm doing. Then we set the value of oldy to whatever y is at the end of the data step. What 'retain' does is tells it that the value of oldy should be 'retained' for the next row so when I compare x to oldy it will be the value from the previous row. Perfect.
    Last edited by Dason; 01-05-2017 at 01:41 PM.
    I don't have emotions and sometimes that makes me very sad.

  5. The Following User Says Thank You to Dason For This Useful Post:

    GretaGarbo (01-05-2017)

+ Reply to Thread

           




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats