BookmarkSubscribeRSS Feed
nickspencer
Obsidian | Level 7
Hi,
I have a SAS dataset that has a column AMT which is a numeric variable and has format 15.2 . I want to convert it to a character variable and also add a dollar sign. I did final_amt=put(AMT,dollar15.2) and it worked but is it the right way to do? Or is there any other correct way to do? I always thought the informat is to read the variable rather than to display but I am confused I am able to display in dollar format with informat.

Any suggestions or insight is appreciated.

Thanks
4 REPLIES 4
Astounding
PROC Star

In terms of the syntax you did it correctly.  However, if you started with extremely large values, you may not have converted properly.

 

SAS formats specify the entire width.  For example, 15.2 does NOT mean 15 digits before the decimal point, plus 2 digits after.  It means a total of 15 characters including digits before the decimal point, digits after, and the decimal point itself.  It also includes a negative sign if one is needed.

 

If your data actually contains numbers as large as 1 trillion, then the width of 15 would not be enough when using a dollar15.2 format.  This number requires a width of 17:

 

$1,222,333,444.00

 

The original format of 15.2 makes me think that you might need a width of 15 without the commas and dollar sign.  If that is the case, you would need a wider format when adding commas and a dollar sign, such as dollar19.2

PaigeMiller
Diamond | Level 26

If you just want to display the variable AMT as a dollar sign plus some numbers, assign a format to it. No need to create a new variable.

--
Paige Miller
ballardw
Super User

I agree with @PaigeMiller that there is likely very little reason to create a character variable for most uses. One real reason not to is that values do not sort as expected.

data example;
   length x $ 15;
   x="$39.54";output;
   x="$1,000,000.00"; output;
run;

proc sort data=example;
  by x;
run;

proc print data=example;
run;

which would imply that a million is less than 39.

You could end up spending a lot of time playing with output to get things to appear the way you want in report and analysis procedures.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 4007 views
  • 2 likes
  • 5 in conversation