DATA Step, Macro, Functions and more

How to convert any date or numeric field into character in sas

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

How to convert any date or numeric field into character in sas

[ Edited ]

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.


Accepted Solutions
Solution
‎11-20-2017 12:29 AM
Esteemed Advisor
Posts: 5,482

Re: How to convert any date or numeric field into character in sas

[ Edited ]
Posted in reply to laxmikanthr

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;

 

PG

View solution in original post


All Replies
Super User
Posts: 23,342

Re: How to convert any date or numeric field into character in sas

Posted in reply to laxmikanthr

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.


 

Solution
‎11-20-2017 12:29 AM
Esteemed Advisor
Posts: 5,482

Re: How to convert any date or numeric field into character in sas

[ Edited ]
Posted in reply to laxmikanthr

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;

 

PG
New Contributor
Posts: 3

Re: How to convert any date or numeric field into character in sas

Thanks for the solution

Super User
Super User
Posts: 7,941

Re: How to convert any date or numeric field into character in sas

Posted in reply to laxmikanthr

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
New Contributor
Posts: 3

Re: How to convert any date or numeric field into character in sas

Thanks a lot for the valuable solution it is working

Super User
Posts: 13,358

Re: How to convert any date or numeric field into character in sas

Posted in reply to laxmikanthr

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.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 169 views
  • 4 likes
  • 5 in conversation