BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
djbateman
Lapis Lazuli | Level 10

Is there a way to define a format that keeps a variable as numeric type but displays a character string much like the dollarw.d or percentw.d format?

For example, if the value stored is "1234.56" but is linked to format dollar7.2, then the value will appear as "$1,234.56" (technically a character string due to '$' and ',' but still numeric).  Or if the value stored is "0.1234" and is linked to format percent9.1, then the value will appear as "12.3%" (technically a character string due to '%' but still numeric).

Now, if I have a numeric valued stored as "1.23" with a user-defined format, can I make it appear as "1.23 mg/m2" and still retain the numeric properties?  I used to create a character variable by using:  charvar=compress(numvar) || ' mg/m2'; but I  want to retain the properties of a numeric variable so I can sort numerically and/or summarize the data while automatically displaying any units that should be attached.

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

You could always create a picture format.  Take a look at: http://www2.sas.com/proceedings/sugi31/243-31.pdf

View solution in original post

3 REPLIES 3
art297
Opal | Level 21

You could always create a picture format.  Take a look at: http://www2.sas.com/proceedings/sugi31/243-31.pdf

djbateman
Lapis Lazuli | Level 10

I knew there would be something simple in PROC FORMAT, but I wasn't finding it.  This did the trick.  Thanks!

ballardw
Super User

Picture formats let you do such things as add suffix or prefixes to numbers using a combination of digit selectors and directives. The example below comes close. Use of 'mg/m2' treats the 2 as a digit selector and replaces the 2 with a 0, or possibly another digit depending on range of the numeric involved.

 

proc format library=work;

picture mg

low-high = '000009.99 mg/msq';

run;

data _null_;

x=1.23;

put x mg.;

run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1759 views
  • 3 likes
  • 3 in conversation