<?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: Number format which depending on the data includes a decimal or not in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752806#M237165</link>
    <description>&lt;P&gt;Dear Bruno&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The proc report code appears to be the solution that I am looking for. Thank you very much for providing a good example of how the code works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;</description>
    <pubDate>Thu, 08 Jul 2021 08:54:31 GMT</pubDate>
    <dc:creator>mgrasmussen</dc:creator>
    <dc:date>2021-07-08T08:54:31Z</dc:date>
    <item>
      <title>Number format which depending on the data includes a decimal or not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752508#M237057</link>
      <description>&lt;P&gt;Dear SAS expert&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am wondering whether there is a format which supports the following.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Based on this example data:&lt;/P&gt;&lt;P&gt;1.000.200&lt;/P&gt;&lt;P&gt;200,25&lt;/P&gt;&lt;P&gt;10&lt;/P&gt;&lt;P&gt;500,3&lt;/P&gt;&lt;P&gt;10,03&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to identify a format which includes one decimal when a number is not an integer and also when a number is not rounded to ,0. I would also like to include a thousand seperator as shown in 1st observation above. So with the example data I would like to have it presented in the following way:&lt;/P&gt;&lt;P&gt;1.000.200&lt;/P&gt;&lt;P&gt;200,3&lt;/P&gt;&lt;P&gt;10&lt;/P&gt;&lt;P&gt;500,3&lt;/P&gt;&lt;P&gt;10&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does anyone know of such a format? I have worked a lot in Stata where such a format was specified in the code using the letter g (general).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;General&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 09:54:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752508#M237057</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2021-07-07T09:54:02Z</dc:date>
    </item>
    <item>
      <title>Re: Number format which depending on the data includes a decimal or not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752525#M237070</link>
      <description>&lt;P&gt;You can create your own format and use a function to create the formatted value.&lt;/P&gt;
&lt;P&gt;You can find more on in the doc here:&amp;nbsp;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/n1eyyzaux0ze5ln1k03gl338avbj.htm" target="_blank"&gt;https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/proc/n1eyyzaux0ze5ln1k03gl338avbj.htm&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;An example for this (change the logic according to your needs):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
 * create the function
 */
proc fcmp outlib=work.myformats.mypkg;
function generalFormat(number) $ 32;
  length returnValue $ 32;
  select;
    when ( number = int(number) ) do;
      returnValue = put(number, commax32.);
    end;
  when ( scan( put(round(number, 0.1), 32.1), -1, ".") = "0" ) do;
    returnValue = put(number, commax32.);
  end;
  otherwise do;
    returnValue = put(number, commax32.1);
  end;
  end;
  return (left(returnValue));
endsub;
run;

/*
 * make the new function available
 */
options cmplib=work.myformats;

/*
 * create some data and test the function
 */
data have;
do value = 1000200, 200.25, 10, 500.3, 10.03;
  value2 = value;
  /* test out just the function */
  newValue = generalFormat(value);
  output;
end;
run;

/*
 * create a format using the function
 */
proc format;
value gfmt (default=32)
  low - high = [generalFormat()] 
;
run;

/*
 * test it out
 */
proc print data=have;
  format value2 gfmt.;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But why have some numbers with decimals and others not? Makes reading more difficult, I think.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 12:02:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752525#M237070</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2021-07-07T12:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Number format which depending on the data includes a decimal or not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752540#M237077</link>
      <description>&lt;P&gt;Hey Bruno&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your reply.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is a bit more complicated code than what I hoped for, but thank you providing it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For your question at the end then yes, it does seem strange. My issue is that I have a numeric variable which includes both frequencies and percentages (organized via a categorical variable). I should correct my original post; only integer values (frequencies) should be listed without a decimal, whereas percentages should be listed with one decimal (irrespective of whether they are actually integers).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Martin&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 12:41:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752540#M237077</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2021-07-07T12:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: Number format which depending on the data includes a decimal or not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752582#M237095</link>
      <description>&lt;P&gt;Since a format attached to a variable applies to all of the observations you cannot really do what you want in the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now if you are making a report out of the data then you can have more control.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jul 2021 14:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752582#M237095</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-07-07T14:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Number format which depending on the data includes a decimal or not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752599#M237108</link>
      <description>&lt;P&gt;As already mentioned a format just sees the number and nothing else.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use Proc REPORT to do what you want, use a different format depending on some other variable content.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have a look at this example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data newclass;
  set sashelp.class;
  hw_ratio = height / weight;
  if sex = "M" then do;
    hw_ratio = hw_ratio * 10000;
  end;
run;

proc report data=newclass;
  columns name sex height weight hw_ratio _dummy;
  define height / display;
  define  weight / display;
  define hw_ratio / display;
  define _dummy / noprint;
  compute _dummy;
    if sex = "M" then do;
      call define("hw_ratio", "format", "commax14.");
    end;
    else do;
      call define("hw_ratio", "format", "percent9.1");
      call define (_row_, "style", "style={background=lightgray}");
    end;
  endcomp;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jul 2021 14:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752599#M237108</guid>
      <dc:creator>BrunoMueller</dc:creator>
      <dc:date>2021-07-07T14:50:28Z</dc:date>
    </item>
    <item>
      <title>Re: Number format which depending on the data includes a decimal or not</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752806#M237165</link>
      <description>&lt;P&gt;Dear Bruno&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The proc report code appears to be the solution that I am looking for. Thank you very much for providing a good example of how the code works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Martin&lt;/P&gt;</description>
      <pubDate>Thu, 08 Jul 2021 08:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Number-format-which-depending-on-the-data-includes-a-decimal-or/m-p/752806#M237165</guid>
      <dc:creator>mgrasmussen</dc:creator>
      <dc:date>2021-07-08T08:54:31Z</dc:date>
    </item>
  </channel>
</rss>

