## where is the mistake

Solved
Occasional Contributor
Posts: 14

# where is the mistake

[ Edited ]

simple code

val2 = (x - lag1(x));
val3 = (lag1(x) - lag2(x));

if val2 = val3 then val1 = 0;

why once the code works (val1 = 0   (-0,004 = -0,004)) and once (val1 =. (-0,00025 = -0,00025))

I do not understand and I can not fix it

Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 21,596

## Re: where is the mistake

Numerical precision. It's a think with how computers stores numbers.

Round the values to whatever decimal point you want to use before your comparison using the ROUND function.

``if round(val2, 0.00001) = round(val3, 0.00001) then ...``

http://documentation.sas.com/?docsetId=lrcon&docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&docsetVer...

PS this is an issue in most software packages, everyone encounters it at some point

All Replies
Super User
Posts: 21,596

## Re: where is the mistake

I'm not typing out data from images.

Can you please produce a reproducible example of the issue you're talking about, including sample data, your full code and log?

You can create sample data using the techniques here:

https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat...

Or a simple data step to read the values in your word doc will suffice.

The usual issue people run into with LAG is not realizing it's a queue not really the last value. And using conditionally with an IF statement but I don't see any of that in your code.

makset7 wrote:

simple code

val2 = (x - lag1(x));
val3 = (lag1(x) - lag2(x));

if val2 = val3 then val1 = 0;

why once the code works (val1 = 0   (-0,004 = -0,004)) and once (val1 =. (-0,00025 = -0,00025))

I do not understand and I can not fix it

Occasional Contributor
Posts: 14

## Re: where is the mistake

[ Edited ]

example

change example.doc to example.sas7bdat

Super User
Posts: 21,596

## Re: where is the mistake

Ok, it works as expected to me. What are you expecting that you're not achieving with this code?

What, exactly, is your desired output from this input data set?

Occasional Contributor
Posts: 14

## Re: where is the mistake

if val2 = val3 then val1 = 0;

if _n_ = 3  val1 = 0;

if _n_ = 6  val1 = .;

why?????????

Solution
2 weeks ago
Super User
Posts: 21,596

## Re: where is the mistake

Numerical precision. It's a think with how computers stores numbers.

Round the values to whatever decimal point you want to use before your comparison using the ROUND function.

``if round(val2, 0.00001) = round(val3, 0.00001) then ...``

http://documentation.sas.com/?docsetId=lrcon&docsetTarget=p0ji1unv6thm0dn1gp4t01a1u0g6.htm&docsetVer...

PS this is an issue in most software packages, everyone encounters it at some point

Super User
Posts: 12,148

## Re: where is the mistake

makset7 wrote:

example

change example.doc to example.sas7bdat

Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

Binary SAS data sets are not always useable by everyone due to language or character encoding, version (SAS9.2 can have issues with 9.4 sets) and sometime operating system. The data step code will create a version anyone can use.

And you still haven't show the entire code.

Occasional Contributor
Posts: 14

## Re: where is the mistake

data example;

set example;

val2 = (x - lag1(x));
val3 = (lag1(x) - lag2(x));

run;

data example;

set example;

if val2 = val3 then val1 = 0;

run;

Super User
Posts: 12,148

## Re: where is the mistake

Post your entire data step. The LAG and DIF functions ( val2 = (x - lag1(x)); is the same as val2 = DIF1(x) have separate results depending on if you use any conditional statements such as:

if somevar > 10 then  val2 = (x - lag1(x));

which lag only looks at the last record when the condition was true.

So when "if" is used likely you need to create temporary variables to hold the lagged values and use those such as:

L1x= lag(x);

L2x= lag(x);

if somevar > 10 then do;

var2 = x- L1x;

var3 = L1x-L2X;

end;

drop L1x L2x:

☑ This topic is solved.