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

Im sure this might be an easy question that we cant see right now being in the midst of problem, but we are summing a numeric variable using a number of methods (sum function sql, proc means, data step with retain) by an id.

Looking at the raw data there appears to be nothing beyond two decimal places in the data. We confirm this after getting it in sas by taking the difference of the variable round to 2 places and the variable without rounding and get no differences between the two.

However after we sum by any of the methods and we take the difference between the rounded sum and the actual sum we are getting differences.

We cant figure out why this is happening or if we are causing it somehow.


Thanks


EJ

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

http://support.sas.com/techsup/technote/ts654.pdf


17         data _null_;
18            array a[10] (10*.1);
19            retain b 1;
20            c = sum(of a
  • );
  • 21            put (b c)(=hex16.);
    22            put (b c)(=best32.);
    23            run;

    b=3FF0000000000000 c=3FEFFFFFFFFFFFFF
    b=
    1 c=1

    View solution in original post

    7 REPLIES 7
    Haikuo
    Onyx | Level 15

    Eric, not sure if this is your case, but it has been long known that there could be some precision issue if 'minus' operation or summary with negative numbers involves.  Please see the following example:

    data _null_;

    a=2.16;b=-1.17;

    raw_c=sum(a,b);

    round_c=round(raw_c,0.01);

    put raw_c= round_c=;

    if raw_c=round_c then put "equal" ; else put "not equal";

    run;

    They are indeed not equal to SAS if you put hex20. to check it out. The safe way to deal with it is to use round() to maintain the precision level.

    Haikuo

    Astounding
    PROC Star

    Another approach to dealing with the problem:

    Multiply all values by 100.  Round to the nearest integer.  Sum.  Divide the sum by 100.

    Precision issues are nearly nonexistent when dealing with integers. 

    esjackso
    Quartz | Level 8

    Thanks ! Something like this is where we will end up Im sure. We are actually trying to validate a stored procedure in SQL using SAS so we were trying to reduce the rounding as much as possible to eliminate steps were mismatching can occur.

    EJ

    esjackso
    Quartz | Level 8

    Thanks for the response but our values are all positive or zero.

    EJ

    data_null__
    Jade | Level 19

    http://support.sas.com/techsup/technote/ts654.pdf


    17         data _null_;
    18            array a[10] (10*.1);
    19            retain b 1;
    20            c = sum(of a
  • );
  • 21            put (b c)(=hex16.);
    22            put (b c)(=best32.);
    23            run;

    b=3FF0000000000000 c=3FEFFFFFFFFFFFFF
    b=
    1 c=1
    esjackso
    Quartz | Level 8

    Thanks everyone ... I thought it was something like that but I couldnt explain it very well but the technote was perfect

    EJ

    sas-innovate-2024.png

    Don't miss out on SAS Innovate - Register now for the FREE Livestream!

    Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

     

    Register now!

    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.

    Click image to register for webinarClick image to register for webinar

    Classroom Training Available!

    Select SAS Training centers are offering in-person courses. View upcoming courses for:

    View all other training opportunities.

    Discussion stats
    • 7 replies
    • 3228 views
    • 0 likes
    • 4 in conversation