BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
laxmikanthr
Calcite | Level 5

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.

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

6 REPLIES 6
Reeza
Super User

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.


 

PGStats
Opal | Level 21

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
laxmikanthr
Calcite | Level 5

Thanks for the solution

Tom
Super User Tom
Super User

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
laxmikanthr
Calcite | Level 5

Thanks a lot for the valuable solution it is working

ballardw
Super User

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.

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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