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
thank you in advance for your help
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 ...
PS this is an issue in most software packages, everyone encounters it at some point 🙂
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:
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
thank you in advance for your help
example
change example.doc to example.sas7bdat
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?
if val2 = val3 then val1 = 0;
if _n_ = 3 val1 = 0;
if _n_ = 6 val1 = .;
why?????????
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 ...
PS this is an issue in most software packages, everyone encounters it at some point 🙂
@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.
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;
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:
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.