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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 5 replies
  • 1147 views
  • 0 likes
  • 4 in conversation