BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
makset7
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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 🙂

View solution in original post

8 REPLIES 8
Reeza
Super User

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

 

thank you in advance for your help


 

makset7
Obsidian | Level 7

example

 

change example.doc to example.sas7bdat

Reeza
Super User

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?

makset7
Obsidian | Level 7

if val2 = val3 then val1 = 0;   

 

if _n_ = 3  val1 = 0;

if _n_ = 6  val1 = .;

 

why?????????

Reeza
Super User

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 🙂

ballardw
Super User

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

makset7
Obsidian | Level 7

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;

ballardw
Super User

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 2000 views
  • 0 likes
  • 3 in conversation