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

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
Quartz | Level 8 K_S
Quartz | Level 8

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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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