BookmarkSubscribeRSS Feed
ashkum
Calcite | Level 5

 I have tried many times  to convert date  into sas time value but do not get the result. I am sharing my syntax  please 

correct me where I am wrong. All the syntax executed perfectly except last one i.e final although it has converted from character to numeric type but its showing blank row.
data test;
infile datalines;
input id$ date;
datalines;
1001   505 
1002   1023
;
run;
here dates are given into numeric type I want to convert into 05:05 and 10:23 hour and minute format.
 
data test1;
set test;
char = put(date, z4.);
hour = substr(char,1,2);
min = substr(char,3,2);
con = trim(hour)||':'||trim(min);
final = input(con,best12.);
run;
5 REPLIES 5
SuryaKiran
Meteorite | Level 14

In your INPUT you need to give TIME5. informat.

 

data test1;
format final time5.;
set test;
char = put(date, z4.);
hour = substr(char,1,2);
min = substr(char,3,2);
con = trim(hour)||':'||trim(min);
final = input(con,time5.);
run;
Thanks,
Suryakiran
ashkum
Calcite | Level 5

thanks a ton....SuryaKiran

Do you have any other syntax to solve this problem...

this method looks lengthy  

SuryaKiran
Meteorite | Level 14

You can avoid creating unnecessary new variables and can create only one by combining all the function.

 

data test1;
format time time5.;
set test;
Time=input(CATX(":",substr(put(date, z4.),1,2),substr(put(date, z4.),3,2)),TIME5.);
run;

I don't think this is a lengthy process. If your data is not formatted properly then you need to go for all these extra.  

Thanks,
Suryakiran
mkeintz
PROC Star

Why not keep FINAL as a sas numeric time value (i.e. the number of seconds after midnight)?  You can apply a format to display it as desired.  And because it's a sas time-value you can do all sorts of time-interval calculations, if needed.

 

data test2;
  set test;
  final=  3600*floor(date/100)+ 60*mod(date,100);
  format final time5.0;
run;

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

Why do you call a time value "date"? That will set you down the wrong path to begin with.

 

data want;
   set have;
   time = input(put(date,z4.),hhmmss4.);
   format time time5.;
run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2167 views
  • 0 likes
  • 4 in conversation