If I have a numeric variable with a format, is there a way to get the formatted value as a character variable?
e.g. I would like to write something like the following to print 10/06/2009
to the screen but there is no putformatted()
function.
data test;
format i ddmmyy10.;
i = "10JUN2009"d;
run;
data _null_;
set test;
i_formatted = putformatted(i); /* How should I write this? */
put i_formatted;
run;
(Obviously I can write put(i, ddmmyy10.)
, but my code needs to work for whatever format i
happens to have.)
Use
vformat()
function.Yes, there is a putformatted() function. In fact, there are two: putc() and putn(). Putc handles character formats, putn() numeric. Your code will need to look at the format name (all and only character formats start with "$") do determine which to use. Here is the syntax of putc (from the interactive help):
Arguments
I can do this with macro code and
sashelp.vcolumn
but it's a bit fiddly.The
VVALUE
function formats the variable passed to it using the format associated with the variable. Here's the code usingVVALUE
:While cmjohns solution is slightly faster than this code, this code is simpler because there are no macros involved.
This seemed to work for a couple that I tried. I used VARFMT and a macro function to retrieve the format of the given variable.
This gave me: