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?
@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
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?
If it is not numeric, then it is already a string (character), so there's no need for converting.
@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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.
