BookmarkSubscribeRSS Feed
AndrewHowell
Moderator

Trying to make sense of the "D" numeric format, and when best to use it.

 

And why a "put(12345,D32.9)" yields "          12345.0000000000000000" (16 zeroes)?

 

Why use D32.9 when I could use 32.16?

 

The documentation seems a bit hard to decypher (summary below).

 

Any assistance appreciated - use cases, perhaps?

 

Dx.p format - SAS(R) 9.4 Formats and Informats: Reference

 

Prints numeric values, possibly with a great range of values, lining up decimal places for values of similar magnitude.

 

Details - The Dw.p format writes numbers so that the decimal point aligns in groups of values with similar magnitude. Larger values of p print the data values with more precision and potentially more shifts in the decimal point alignment. Smaller values of p print the data values with less precision and a greater chance of decimal point alignment.

 

Comparisons

  • The BESTw. format writes as many significant digits as possible in the output field, but if the numbers vary in magnitude, the decimal points do not line up.
  • Dw.p writes numbers with the desired precision and more alignment than the BESTw format.
  • The BESTDw.p format is a combination of the BESTw. format and the Dw.p format in that it formats all numeric data, and it does a better job of aligning decimals than the BESTw. format.
  • The w.d format aligns decimal points, if possible, but it does not necessarily show the same precision for all numbers.
3 REPLIES 3
ChrisNZ
Tourmaline | Level 20

The same code yields 19 zeros for me.

%put &=sysscpl &=sysvlong %sysfunc(putn(12345,D32.9));
SYSSCPL=X64_SRV12 SYSVLONG=9.04.01M2P072314 12345.0000000000000000000

AndrewHowell
Moderator

@ChrisNZ,

 

I've tried 32.0 up to 32.9 (32.0 is same as 32.3). The result is still MORE decimals than 

 

So I guess I'm looking for clear documentation on what the "D" format is actually FOR, and how to know - with certainty - what the output would be for various values of width and/or decimal.

 

Background: I'm storing the result in strings, and I need to know the length of the strings and the placement of the decimal point.

 

ChrisNZ
Tourmaline | Level 20

 I need to know the length of the strings and the placement of the decimal point.

Why use format Dw.p then?  

Its description

Prints numeric values, possibly with a great range of values, lining up decimal places for values of similar magnitude.

is enticing for your purpose, but the small-print caveat

Smaller values of p print the data values with less precision and a greater chance of decimal point alignment.

includes the word chance, which is not what you seem to want.

 

A fixed format like w.d seems more suitable for your purpose, doesn't it?

 

I never used format Dw.p before, so am unsure how precision parameter works. As you rightly point out, the documentation is rather terse on the matter.

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 926 views
  • 0 likes
  • 2 in conversation