Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- basic arithmetic result not logical

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 04-01-2016 12:16 AM
(889 views)

Hi,

I just did a practice of array and happened to find that one arithmetic result is not logical.

```
**** data preparation ;
data visits;
infile datalines;
input patid visdt0 visdt1 visdt2 visdt3 visdt4;
datalines;
1001 0.1 1.1 2.1 3.1 4.1
1002 0.2 1.2 2.2 3.2 4.2
;
run;
**** foolproof dataset;
data work.checkvisits;
set work.visits (keep=patid visdt0 visdt1 visdt2 visdt3 visdt4);
array visdt(1:5) visdt0 visdt1 visdt2 visdt3 visdt4;
do i=2 to 5;
x = visdt(i) - visdt(i-1);
if x gt 1 then do;
* there should be no output since all differences;
* should equal to 1 ;
put visdt(i-1)= visdt(i)= x=;
output;
end;
end;
drop i x;
run;
**** print ;
proc print data=checkvisits;
run;
```

and the log:

```log

visdt1=1.2 visdt2=2.2 x=1

NOTE: There were 2 observations read from the data set WORK.VISITS.

NOTE: The data set WORK.CHECKVISITS has 1 observations and 6 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

```

The environment is SAS 9.2 on Linux. Please advise, thanks.

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Numerical precision.

Although reference is 9.4 it does apply to 9.2 as well

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Numerical precision.

Although reference is 9.4 it does apply to 9.2 as well

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Thanks so much Reeza, I adjusted a bit the program and found two remainings do not match the real value probably due to truncation in binary numbers.

13 **** foolproof dataset; 14 data work.checkvisits; 15 set work.visits; 16 array visdt(1:5) visdt0 visdt1 visdt2 visdt3 visdt4; 17 do i=2 to 5; 18 * the expeced result should be 0 ; 19 x = visdt(i) - visdt(i-1) - 1; 20 put visdt(i-1)= visdt(i)= x=; 21 if x gt 0 then do; 22 output; 23 end; 24 end; The SAS System 25 drop i x; 26 run; visdt0=0.1 visdt1=1.1 x=0 visdt1=1.1 visdt2=2.1 x=0 visdt2=2.1 visdt3=3.1 x=0 visdt3=3.1 visdt4=4.1 x=-4.44089E-16 visdt0=0.2 visdt1=1.2 x=0 visdt1=1.2 visdt2=2.2 x=2.220446E-16 visdt2=2.2 visdt3=3.2 x=0 visdt3=3.2 visdt4=4.2 x=0 NOTE: There were 2 observations read from the data set WORK.VISITS. NOTE: The data set WORK.CHECKVISITS has 1 observations and 6 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Those are the exact types of issues that arise with numerical precision for decimals. If you do something like

x = round( visdt(i) - visdt(i-1), 0.00001) -1;

you may be happier.

**Available on demand!**

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

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.