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

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

9 REPLIES 9
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
ballardw
Super User

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;
novinosrin
Tourmaline | Level 20

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

FreelanceReinh
Jade | Level 19

@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.

novinosrin
Tourmaline | Level 20

Thanks a ton @FreelanceReinh . Much needed and appreciated!!!!! 🙂 

Astounding
PROC Star

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.

ballardw
Super User

@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.

 

@novinosrin

 

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.

novinosrin
Tourmaline | Level 20

Bravo! Thank you sir @ballardw

Ksharp
Super User

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;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 9 replies
  • 6932 views
  • 19 likes
  • 7 in conversation