Hello all -
I am looking for a format that formats a numeric without rounding. For example, I want to display the number 1.5 as 1 NOT 2.
I do not want to use a function to create a new variable. I want to retain the value but want the format to use truncation rather than rounding when displaying.
Does this require a custom format?
Thanks
You can use a functions in a custom format definition:
proc format library=work; value floor other= [floor()]; run; data example; x=1.23; format x floor.; run;
I do not want to use a function to create a new variable. I want to retain the value but want the format to use truncation rather than rounding when displaying.
As always, I recommend what I think is the best and easiest solution. The FLOOR function does exactly what you want. It retains the full value in the original variable, while creating a new truncated variable. Perhaps there's a good reason why you absolutely have to use a format for this, but I can't think of any.
You can use a functions in a custom format definition:
proc format library=work; value floor other= [floor()]; run; data example; x=1.23; format x floor.; run;
Wow. Thank you for sharing and letting me learn something new. Can you please elaborate how that works if you a min or two ?
Or can you point me to a documentation that explains that
@novinosrin wrote:
Or can you point me to a documentation that explains that
Hi @novinosrin,
As has been mentioned, the Procedures Guide focuses on examples with user-defined functions. Carpenter's Guide to Innovative SAS® Techniques has a 4-page section about this topic (sect. 12.5.5, pp. 384-388) including several examples using SAS-supplied functions.
Thanks a ton @FreelanceReinh . Much needed and appreciated!!!!! 🙂
This is such a good answer that I hate to add anything to it. But just note ...
If your data might contain negative numbers, you should consider INT rather than FLOOR. They will give different results.
@Astounding wrote:
This is such a good answer that I hate to add anything to it. But just note ...
If your data might contain negative numbers, you should consider INT rather than FLOOR. They will give different results.
Yep. I thought about mentioning that the first time but without better examples of data didn't want to confuse the OP too much.
You can use any function that will return a single value for target of a format. The [ ] indicate special handling as either a function or a format could be in the brackets. Useful for different ranges, as in the INT, FLOOR, CEIL, FUZZ or other function. Using the range "other" makes it the default action for the entire range of values. The function is called with no actual value in the argument, i.e. floor() because the Proc Format code will apply the current value of the variable being formatted. Note that this approach is restricted to a single value only, no functions with two or more parameters unless there is a default for everything except the first.
The fun part is that you can use a custom format created with Proc FCMP to do very odd things to values that are completely opaque to a user if they don't have the code for Proc FCMP. The online documentation for Proc Format includes an example where you can create conversions of Celsius to Fahrenheit temperatures or vice versa.
Bravo! Thank you sir @ballardw
PICTURE can do that either.
If X is negative, you will get different result.
proc format;
picture fmt
0-high='00000000'
low-<0='00000000'(prefix='-');
run;
data example;
x=-123.786;
format x fmt.;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.