SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Lag Statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

Lag Statement

I have a large data set that I am trying to use a lag statement that is nested within an if statement. I have attached a file that has the code and also an example of what the output looks like.

 

The lag statment is returning the previous weight and then using this weight to calculate a difference. The lag statment is nested within an if statement based on animal ID. If the previous ID doesn't match, a "." is returned. It is working with the exception of the 2nd observation. I know the first observation for each animal will be a ".". But the first two are ".". Any help is appreacited.

 

Thanks.


Accepted Solutions
Solution
‎09-14-2016 01:22 PM
Super User
Posts: 5,085

Re: Lag Statement

Yes, that's the right tool.  Use an IF/THEN statement.

View solution in original post


All Replies
Super User
Posts: 5,085

Re: Lag Statement

LAG does not retrieve the value from the previous observation.  That's a common mistake.  LAG retrieves the value from the last time that LAG executed.  So it becomes important (in most applications) to make sure that LAG executes on every observation.

 

The usual fix is to execute LAG on every observation by taking it out of any IF THEN statements.  Then on the first observation for each animal ID, reset your new variable (the one that holds the results of the LAG function) to a missing value.

Occasional Contributor
Posts: 13

Re: Lag Statement

Thanks for your reply.

 

I am struggeling on how to reset a variable to a missing value? I have never done this without using some type of if statement.

 

I appreciate your time.

Solution
‎09-14-2016 01:22 PM
Super User
Posts: 5,085

Re: Lag Statement

Yes, that's the right tool.  Use an IF/THEN statement.

Occasional Contributor
Posts: 13

Re: Lag Statement

Thank you so much!

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 512 views
  • 1 like
  • 2 in conversation