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

I have a time variable that looks like this 08:00 and I want it to turn into a string variable that looks the same way.

How would you go about coding this?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@K_S wrote:

Thanks. The time variable I have is not numeric. It is a time variable and the numeric formats don't work on it. Any other tips?


SAS only has two data types. Floating point numbers and fixed length character strings. If the variable actually has time values then it contains the number of seconds since midnight.  It would only look like '08:00' if you were displaying the value with the TIME5. format.

1335  data _null_;
1336    now=time();
1337    put now= time5. now= comma12. ;
1338  run;

now=11:27 now=41,265

 So to convert it to the formatted value use the PUT() or PUTN() function. 

data test;
  input time time. ;
  string1=put(time,time5.);
  string2=putn(time,'time5.');
  format time time5.;
  string3=vvalue(time);
  string4=vvaluex('time');
  put (_all_) (=/);
cards;
08:00
11:27
;
1363  data test;
1364    input time time. ;
1365    string1=put(time,time5.);
1366    string2=putn(time,'time5.');
1367    format time time5.;
1368    string3=vvalue(time);
1369    string4=vvaluex('time');
1370    put (_all_) (=/);
1371  cards;


time=8:00
string1=8:00
string2=8:00
string3=8:00
string4=8:00

time=11:27
string1=11:27
string2=11:27
string3=11:27
string4=11:27

 

View solution in original post

4 REPLIES 4
K_S
Obsidian | Level 7 K_S
Obsidian | Level 7

Thanks. The time variable I have is not numeric. It is a time variable and the numeric formats don't work on it. Any other tips?

Tom
Super User Tom
Super User

@K_S wrote:

Thanks. The time variable I have is not numeric. It is a time variable and the numeric formats don't work on it. Any other tips?


SAS only has two data types. Floating point numbers and fixed length character strings. If the variable actually has time values then it contains the number of seconds since midnight.  It would only look like '08:00' if you were displaying the value with the TIME5. format.

1335  data _null_;
1336    now=time();
1337    put now= time5. now= comma12. ;
1338  run;

now=11:27 now=41,265

 So to convert it to the formatted value use the PUT() or PUTN() function. 

data test;
  input time time. ;
  string1=put(time,time5.);
  string2=putn(time,'time5.');
  format time time5.;
  string3=vvalue(time);
  string4=vvaluex('time');
  put (_all_) (=/);
cards;
08:00
11:27
;
1363  data test;
1364    input time time. ;
1365    string1=put(time,time5.);
1366    string2=putn(time,'time5.');
1367    format time time5.;
1368    string3=vvalue(time);
1369    string4=vvaluex('time');
1370    put (_all_) (=/);
1371  cards;


time=8:00
string1=8:00
string2=8:00
string3=8:00
string4=8:00

time=11:27
string1=11:27
string2=11:27
string3=11:27
string4=11:27

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 4 replies
  • 2498 views
  • 2 likes
  • 3 in conversation