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

Hello everyone,

 

Not a very important question but I was wondering if there were a format or workaround to display values without decimals when they are integers (like BESTw.), and with a period that separates every three digits and a comma that separates the decimal fraction (like COMMAw.d)

 

Best,

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

@Alain38, I think this gives you what you want

 

proc fcmp outlib=work.functions.fun; 
  function fnc(num) $;
    if num=int(num) then r=scan(strip(put(num, commax32.10)), 1, ',');
    else                 r=tranwrd(strip(tranwrd(strip(put(num, commax32.10)), "0", " "))," ","0");
    return (r);
  endsub;
run;

options cmplib=work.functions;    
proc format;
  value fmt (default=32) other=[fnc()];
run;

data test;
   x=1234.567;output; 
   x=-1234.00;output; 
   x=1234567 ;output;
   x=1.23    ;output;   

   format x fmt.;
run;

View solution in original post

10 REPLIES 10
Tom
Super User Tom
Super User

Not that I know of.  You might be able to create one using PROC FCMP so that you can test if the value is an integer and use a different format in those cases.

Alain38
Quartz | Level 8

@Reeza  A period for three digits and a comma for the decimals is indeed the French format obtained with COMMAXw.d, my mistake in the description sorry. What I was looking for is a nice looking format (such as COMMA or COMMAX) with no decimals displayed when integers (like BEST)

 

@TomThank you for confirming that there is no format doing this ready for use. I will look how to build it with PROC FCMP. Thank you for the example of this in your link @Reeza

Reeza
Super User
You can try using a nested format along with PROC FCMP then. Do you want the variable as a character or numeric? If character this is quite easy within a single line, otherwise it's slightly more work.
Alain38
Quartz | Level 8

Need it numeric... but this is just to improve the display so not vital. I will look into that but if this is really too complicated, I will not do it. Thank you for your answers 🙂

Tom
Super User Tom
Super User

If the range of values is not too large you could just list all of the integers in the range.

data formats ;
  retain fmtname 'mycomma' type 'n' hlo 'f  ' label 'commax8. ';
  do start=-999 to 9999 ;
    output;
  end;
  start=.;
  hlo='fo';
  label='commax8.2';
  output;
  stop;
run;
proc format cntlin=formats;
run;

data test;
  do x=1 to 3,12.5,1234,-300,3456.1;
   output;
  end;
run;
proc print data=test;
 format x mycomma.;
run;
Obs       x

 1            1
 2            2
 3            3
 4        12,50
 5        1.234
 6         -300
 7     3.456,10
Alain38
Quartz | Level 8

Thank you very much @Tom for your help. Unfortunately the range of values is absolutely huge (that's the market value of each U.S. firm, so there are small companies and some huge firms like Facebook...)

PeterClemmensen
Tourmaline | Level 20

@Alain38, I think this gives you what you want

 

proc fcmp outlib=work.functions.fun; 
  function fnc(num) $;
    if num=int(num) then r=scan(strip(put(num, commax32.10)), 1, ',');
    else                 r=tranwrd(strip(tranwrd(strip(put(num, commax32.10)), "0", " "))," ","0");
    return (r);
  endsub;
run;

options cmplib=work.functions;    
proc format;
  value fmt (default=32) other=[fnc()];
run;

data test;
   x=1234.567;output; 
   x=-1234.00;output; 
   x=1234567 ;output;
   x=1.23    ;output;   

   format x fmt.;
run;
Alain38
Quartz | Level 8

@PeterClemmensen  Thank you very much it's working perfectly! Just to be sure to understand the code, I adapted it to the English format if someone needs (personnaly I need both):

 

proc fcmp outlib=work.functions.fun; 
	function fnc(num) $;
		if num=int(num) then r=scan(strip(put(num, comma32.10)), 1, '.');
		else r=tranwrd(strip(tranwrd(strip(put(num, comma32.10)), "0", " "))," ","0");
		return (r);
	endsub;
run;
PeterClemmensen
Tourmaline | Level 20

Glad to help 🙂 Can't test it right not, but it looks right to me.

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 10 replies
  • 5717 views
  • 8 likes
  • 4 in conversation