DATA Step, Macro, Functions and more

Transform time8. to $CHAR8.

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 110
Accepted Solution

Transform time8. to $CHAR8.

I would like to transform a time8. Dataset column to a $CHAR8. Column to represent, for example, 15:47:39 as the same string/$CHAR8. Not SAS’s internal representation. I tried:

 

 

SomeTime=put(SomeTime,time8.);
format SomeTime $CHAR8.;

 

in my dataset without success. Any ideas? Thanks!


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 7,846

Re: Transform time8. to $CHAR8.

Posted in reply to csetzkorn

csetzkorn wrote:

I would like to transform a time8. Dataset column to a $CHAR8. Column to represent, for example, 15:47:39 as the same string/$CHAR8. Not SAS’s internal representation. I tried:

 

 

SomeTime=put(SomeTime,time8.);
format SomeTime $CHAR8.;

 

in my dataset without success. Any ideas? Thanks!


You always have to create a new variable when you want to switch types.

So you might do

data want;
set have (rename=(sometime=_sometime));
length sometime $8;
sometime = put(_sometime,time8.);
drop _sometime;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Trusted Advisor
Posts: 1,932

Re: Transform time8. to $CHAR8.

Posted in reply to csetzkorn

In general, it should not be necessary, as far as I can think of examples, to turn a SAS time (or date or datetime) value to a character variable. It seems like you are making your life difficult for yourself.

 

However, in your code, the variable SomeTime cannot be both numeric and character. It is numeric. If you want a character variable, you have to give it a different variable name.

SAS Super FREQ
Posts: 3,755

Re: Transform time8. to $CHAR8.

Posted in reply to csetzkorn

Two ways: use PUT or if the variable already has a format, you can use VVALUE to get the formatted valu:

 

data A;
length str str2 $8;
format time time8.;
time = '15:47:39't;
str = put(time, time8.);
str2 = vvalue(time);
run;

proc print; run;
Solution
2 weeks ago
Super User
Posts: 7,846

Re: Transform time8. to $CHAR8.

Posted in reply to csetzkorn

csetzkorn wrote:

I would like to transform a time8. Dataset column to a $CHAR8. Column to represent, for example, 15:47:39 as the same string/$CHAR8. Not SAS’s internal representation. I tried:

 

 

SomeTime=put(SomeTime,time8.);
format SomeTime $CHAR8.;

 

in my dataset without success. Any ideas? Thanks!


You always have to create a new variable when you want to switch types.

So you might do

data want;
set have (rename=(sometime=_sometime));
length sometime $8;
sometime = put(_sometime,time8.);
drop _sometime;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 80 views
  • 3 likes
  • 4 in conversation