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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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