BookmarkSubscribeRSS Feed
abcda
Calcite | Level 5

Is there a way to format a value as accounting to mimic the same format in Excel?

 

For example:

$     0.99

$     1.00 

$   10.00 

$ 100.00

 

(the currency sign is left aligned while the numeric value is right aligned)

 

Thanks!

6 REPLIES 6
HB
Barite | Level 11 HB
Barite | Level 11

I don't think SAS has a format statement that will do that.

 

If you really wanted to, you could turn numbers into character values and append spaces and a dollar sign to the front to make them look like that. But that seems silly.

Astounding
PROC Star

While SAS doesn't have such a format, you can create your own.  Here's one way (you might want to add an extra blank after the dollar sign ... that's up to you):

 

proc format;

picture tst 0-< 10 = ' 9.99' (prefix='$  ')

10 - < 100 = ' 99.99' (prefix='$ ')

100- < 1000 = ' 999.99' (prefix='$');

run;

 

data test;

do i=0, 10, 50, 100; put i tst.; end;

run;

HB
Barite | Level 11 HB
Barite | Level 11

If i do

data stuff;
   input mymoney;
datalines;
5.12
6.45
16.23
12.45
164.25
145.78
;
run;

proc format;
picture moneytest 
0 - < 10 = '    9.99' (prefix='$ ')
10 - < 100 = '   99.99' (prefix='$ ')
100- < 1000 = '	 999.99' (prefix='$ ');
run;

proc print;
	var mymoney;
	format mymoney moneytest.;
run;

I get

                                        Obs    mymoney

                                         1       $ 5.12
                                         2       $ 6.45
                                         3      $ 16.23
                                         4      $ 12.45
                                         5     $ 164.25
                                         6     $ 145.78


But if i do

data newmoney;
	set stuff;
	mycharmoney = put(mymoney,10.3);
	mycharmoney = '$' || mycharmoney;
run;

proc print;
	var mycharmoney;
run;

I get

                                       Obs    mycharmoney

                                        1     $     5.12
                                        2     $     6.45
                                        3     $    16.23
                                        4     $    12.45
                                        5     $   164.25
                                        6     $   145.78

Astounding
PROC Star

@HB

 

Notice that in constructing a picture format, the number of characters in the prefix is supposed to change.  (I inadvertently had removed one of those blanks, but have corrected that in the post above.)  Changing the size of the prefix will line up the dollar signs using the PICTURE format.

HB
Barite | Level 11 HB
Barite | Level 11

 

@Astounding

 

I gotcha now. This "_9.00", "__9.00", "___9.00" doesn't seem to do any good. I think SAS collapses the spaces or something.

But this "$_", "$__", "$___" gets the job done. Cool.

ballardw
Super User

@HB The space difference you are seeing from @Astounding's solution is ODS output slightly reformatting things.

Is going to depend on active style, ods destination and/or font.

 

proc print data=newmoney;
   title "Simple Print";
	var mycharmoney /;
run;title;
proc print data=newmoney;
   title "With fixed width font ";
	var mycharmoney /style(data)=[fontfamily="Courier" ];
run; title;
proc print data=newmoney;
   title "With fixed width font and ASIS";
	var mycharmoney /style(data)=[fontfamily="Courier" ASIS=yes];
run;title;

Differing results:

 

Simple Print

Obs mycharmoney
1 $ 5.12
2 $ 6.45
3 $ 16.23
4 $ 12.45
5 $ 164.25
6 $ 145.78
With fixed width font

Obs mycharmoney
1 $ 5.12
2 $ 6.45
3 $ 16.23
4 $ 12.45
5 $ 164.25
6 $ 145.78
With fixed width font and ASIS

Obs mycharmoney
1
$     5.12
2
$     6.45
3
$    16.23
4
$    12.45
5
$   164.25
6
$   145.78

I have no idea why the forum is rendering that last table with the < > bit but in the default HTML results viewer both the $ and Decimals align which I do not get with the simple print even of the string variable.

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
  • 6 replies
  • 2419 views
  • 4 likes
  • 4 in conversation