Hi all,
I am trying to automate a process in which i need to convert any date or numeric field having any format to character field.
Use the function vformat() to get the format name and apply it with function putn().
data test;
x = 99.99;
t = '16nov2017'd;
dt = '16nov2017:23:11:30'dt;
format t yymmdd10. dt datetime17.;
run;
data testStr;
array a x t dt;
array aStr{3} $32;
set test;
do i = 1 to dim(a);
length fmt $32;
fmt = vformat(a{i});
if missing(fmt) then aStr{i} = put(a{i}, best.);
else aStr{i} = putn(a{i}, fmt);
end;
drop i fmt;
run;
proc print data=testStr noobs; run;
Isn't that what the PUT() function does? Is there some other functionality you need?
@laxmikanthr wrote:
Hi all,
I am trying to automate a process in which i need to convert any date or numeric field having any format to character field.
Use the function vformat() to get the format name and apply it with function putn().
data test;
x = 99.99;
t = '16nov2017'd;
dt = '16nov2017:23:11:30'dt;
format t yymmdd10. dt datetime17.;
run;
data testStr;
array a x t dt;
array aStr{3} $32;
set test;
do i = 1 to dim(a);
length fmt $32;
fmt = vformat(a{i});
if missing(fmt) then aStr{i} = put(a{i}, best.);
else aStr{i} = putn(a{i}, fmt);
end;
drop i fmt;
run;
proc print data=testStr noobs; run;
Thanks for the solution
Not sure what you purpose is, but isn't that what PROC PRINT does?
If you want to store it into a character variable then use the VVALUE(),
charvar=vvalue(anyvar);
or VVALUEX() function.
data want ;
set sashelp.class(obs=3) ;
length vname $32 value $200 ;
do vname='sex','age','height';
value=vvaluex(vname);
output;
end;
keep name vname value ;
run;
Obs Name vname value 1 Alfred sex M 2 Alfred age 14 3 Alfred height 69 4 Alice sex F 5 Alice age 13 6 Alice height 56.5 7 Barbara sex F 8 Barbara age 13 9 Barbara height 65.3
Thanks a lot for the valuable solution it is working
Once a variable is numeric it stays numeric. Your process will have to create new variables.
One questions the actual value of this idea. The results will generally not sort properly and all of the functions to manipulate the values are basically gone.
If the purpose is a report read by people then any of the report procedures such as Print, Report or Tabulate are likely much better approaches than trying to change variables.
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.