<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: 1/3 (one third) in proc format in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800362#M314821</link>
    <description>&lt;P&gt;You can make your own CNTLIN style dataset and pass it to PROC FORMAT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data thirds;
  fmtname='FMT';
  type='N';
  hlo=' ';
  sexcl='N';
  eexcl='Y';
  start=-2; end=1/3; label='A'; output;
  start=end; end=2/3; label='B'; output;
  eexcl='N';start=end; end='1'; label='C'; output;
  sexcl='Y';hlo='H';start=end; end=.; label='D'; output;
run;

proc format cntlin=thirds ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you can use some macro code to generate a reasonable approximation of 1/3 as text.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format fmtlib cntlout=formats2;
value fmt
-2-&amp;lt; %sysfunc(putn(1/3,best15.))='A'
%sysfunc(putn(1/3,best15.))-&amp;lt;%sysfunc(putn(2/3,best15.))='B'
%sysfunc(putn(2/3,best15.))-1='C'
1&amp;lt;-high='D'
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For some strange reason the BEST16 format generates 2/3 with the last digit as 6 instead of 7.&amp;nbsp; Probably something to do with precision of storing digital fractions in binary floating format.&amp;nbsp; So using BEST15. as the format there will be minor difference in the definitions of the two formats if you compare the definitions written to a CNTLOUT dataset.&lt;/P&gt;
&lt;PRE&gt;The COMPARE Procedure
Comparison of WORK.FORMATS with WORK.FORMATS2
(Method=EXACT)

Values Comparison Summary

Number of Variables Compared with All Observations Equal: 19.
Number of Variables Compared with Some Observations Unequal: 2.
Total Number of Values which Compare Unequal: 4.
Maximum Difference: 0.


Variables with Unequal Values

Variable  Type  Len   Label                      Ndif   MaxDif

START     CHAR   16   Starting value for format     2
END       CHAR   16   Ending value for format       2



Value Comparison Results for Variables

__________________________________________________________
           ||  Starting value for format
           ||  Base Value           Compare Value
       Obs ||  START                 START
 ________  ||  ________________      ________________
           ||
        2  ||  0.33333333333333       0.3333333333333
        3  ||  0.66666666666667       0.6666666666667
__________________________________________________________
&amp;#12;
&lt;/PRE&gt;</description>
    <pubDate>Sat, 05 Mar 2022 18:47:40 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-03-05T18:47:40Z</dc:date>
    <item>
      <title>1/3 (one third) in proc format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800358#M314818</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;What is the way to write 1/3 (one third) in proc format?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value Fmt
-2,0-&amp;lt;1/3='A'
1/3-&amp;lt;2/3='B'
2/3-1='C'
-1,1&amp;lt;-high='D'
;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Mar 2022 17:00:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800358#M314818</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-03-05T17:00:56Z</dc:date>
    </item>
    <item>
      <title>Re: 1/3 (one third) in proc format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800361#M314820</link>
      <description>&lt;P&gt;There is no way to write 1/3 exactly.&amp;nbsp; You need to use digits and a decimal point, and there is no exact representation.&amp;nbsp; You can approximate, for example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;0.33333333333 - &amp;lt; 0.66666666667 = 'B'
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also,&amp;nbsp; you may be able to apply the FUZZ option to eliminate gaps between your ranges for A, B, and C.&lt;/P&gt;</description>
      <pubDate>Sat, 05 Mar 2022 18:13:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800361#M314820</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-03-05T18:13:14Z</dc:date>
    </item>
    <item>
      <title>Re: 1/3 (one third) in proc format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800362#M314821</link>
      <description>&lt;P&gt;You can make your own CNTLIN style dataset and pass it to PROC FORMAT.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data thirds;
  fmtname='FMT';
  type='N';
  hlo=' ';
  sexcl='N';
  eexcl='Y';
  start=-2; end=1/3; label='A'; output;
  start=end; end=2/3; label='B'; output;
  eexcl='N';start=end; end='1'; label='C'; output;
  sexcl='Y';hlo='H';start=end; end=.; label='D'; output;
run;

proc format cntlin=thirds ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you can use some macro code to generate a reasonable approximation of 1/3 as text.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format fmtlib cntlout=formats2;
value fmt
-2-&amp;lt; %sysfunc(putn(1/3,best15.))='A'
%sysfunc(putn(1/3,best15.))-&amp;lt;%sysfunc(putn(2/3,best15.))='B'
%sysfunc(putn(2/3,best15.))-1='C'
1&amp;lt;-high='D'
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;For some strange reason the BEST16 format generates 2/3 with the last digit as 6 instead of 7.&amp;nbsp; Probably something to do with precision of storing digital fractions in binary floating format.&amp;nbsp; So using BEST15. as the format there will be minor difference in the definitions of the two formats if you compare the definitions written to a CNTLOUT dataset.&lt;/P&gt;
&lt;PRE&gt;The COMPARE Procedure
Comparison of WORK.FORMATS with WORK.FORMATS2
(Method=EXACT)

Values Comparison Summary

Number of Variables Compared with All Observations Equal: 19.
Number of Variables Compared with Some Observations Unequal: 2.
Total Number of Values which Compare Unequal: 4.
Maximum Difference: 0.


Variables with Unequal Values

Variable  Type  Len   Label                      Ndif   MaxDif

START     CHAR   16   Starting value for format     2
END       CHAR   16   Ending value for format       2



Value Comparison Results for Variables

__________________________________________________________
           ||  Starting value for format
           ||  Base Value           Compare Value
       Obs ||  START                 START
 ________  ||  ________________      ________________
           ||
        2  ||  0.33333333333333       0.3333333333333
        3  ||  0.66666666666667       0.6666666666667
__________________________________________________________
&amp;#12;
&lt;/PRE&gt;</description>
      <pubDate>Sat, 05 Mar 2022 18:47:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800362#M314821</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-03-05T18:47:40Z</dc:date>
    </item>
    <item>
      <title>Re: 1/3 (one third) in proc format</title>
      <link>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800373#M314826</link>
      <description>&lt;P&gt;Thanks, &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. Interesting case. The CNTLIN style dataset achieves the maximum possible accuracy by literally using &lt;FONT face="courier new,courier"&gt;1/3&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;2/3&lt;/FONT&gt; in the definition. I would add&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;fuzz=0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as recommended in the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/p1upn25lbfo6mkn1wncu4dyh9q91.htm#n0qhedcd1ls7ren1hn86ntotxrai" target="_blank" rel="noopener"&gt;documentation of the FUZZ= option&lt;/A&gt;. Otherwise, fuzzing (with the default &lt;FONT face="courier new,courier"&gt;fuzz=1E-12&lt;/FONT&gt;) would interfere with the intended format definition close to the interval borders.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;EM&gt;exact&lt;/EM&gt; &lt;EM&gt;same&lt;/EM&gt; format could be created with&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;'s approach:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value fmt (fuzz=0)
-2                  -&amp;lt; 0.3333333333333333 = 'A'
 0.3333333333333333 -&amp;lt; 0.6666666666666666 = 'B'
 0.6666666666666666 -  1                  = 'C'
 1                  &amp;lt;- high               = 'D'
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but the correct choice of the numeric literals is crucial (and possibly platform-dependent!).&amp;nbsp;Windows SAS 9.4M5 (regardless of system option DECIMALCONV) yields:&lt;/P&gt;
&lt;PRE&gt;792   data _null_;
793   x=1/3;
794   y=2/3;
795   put (x y) (=hex16.);
796   x=0.3333333333333333;
797   y=0.6666666666666666;
798   put (x y) (=hex16.);
799   run;

x=3FD5555555555555 y=3FE5555555555555
x=3FD5555555555555 y=3FE5555555555555&lt;/PRE&gt;
&lt;P&gt;Using any fewer than the above sixteen (!) decimal 3s or 6s would create different internal representations (and hence different formats), as would rounding &lt;FONT face="courier new,courier"&gt;y&lt;/FONT&gt; to&amp;nbsp;&lt;FONT face="courier new,courier"&gt;0.6666666666666667&lt;/FONT&gt;. I think none of the common numeric formats (such as &lt;EM&gt;w&lt;/EM&gt;.&lt;EM&gt;d&lt;/EM&gt;, BEST&lt;EM&gt;w&lt;/EM&gt;., E&lt;EM&gt;w&lt;/EM&gt;., etc.) would create exactly those literals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It's tricky that &lt;EM&gt;neither&lt;/EM&gt; FMTLIB output &lt;EM&gt;nor&lt;/EM&gt; the CNTLOUT dataset are precise enough to reveal the difference between the exact format (as defined above) and a format defined with slightly different literals. In the case of the CNTLOUT dataset (which uses &lt;EM&gt;character&lt;/EM&gt; variables START and END of length &lt;EM&gt;16&lt;/EM&gt;&amp;nbsp;containing rounded decimal representations of the fractions in question) this means that feeding the dataset back with the CNTLIN option creates a different format! See the log below:&lt;/P&gt;
&lt;PRE&gt;896   data _null_;
897   x=1/3;
898   y=2/3;
899   put (x y) (=fmt.);
900   run;

x=B y=C
NOTE: DATA statement used (Total process time):
      real time           0.10 seconds
      cpu time            0.10 seconds


901   proc format cntlout=fmtds;
902   select fmt;
903   run;

NOTE: PROCEDURE FORMAT used (Total process time):
      real time           0.06 seconds
      cpu time            0.06 seconds

NOTE: The data set WORK.FMTDS has 4 observations and 21 variables.

904   proc format cntlin=fmtds;
NOTE: Format FMT is already on the library WORK.FORMATS.
NOTE: Format FMT has been output.
905   run;

NOTE: PROCEDURE FORMAT used (Total process time):
      real time           0.07 seconds
      cpu time            0.07 seconds

NOTE: There were 4 observations read from the data set WORK.FMTDS.

906   data _null_;
907   x=1/3;
908   y=2/3;
909   put (x y) (=fmt.);
910   run;

x=B y=B&lt;/PRE&gt;
&lt;P&gt;In practice, special care should be taken that the &lt;EM&gt;values&lt;/EM&gt; to be formatted are using the appropriate internal representations to ensure correct classification of borderline cases.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 05 Mar 2022 21:27:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/1-3-one-third-in-proc-format/m-p/800373#M314826</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-03-05T21:27:24Z</dc:date>
    </item>
  </channel>
</rss>

