DATA Step, Macro, Functions and more

Convert numeric variable into SAS time variable

Reply
New Contributor
Posts: 2

Convert numeric variable into SAS time variable

 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;
Valued Guide
Posts: 558

Re: Convert numeric variable into SAS time variable

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
New Contributor
Posts: 2

Re: Convert numeric variable into SAS time variable

Posted in reply to SuryaKiran

thanks a ton....SuryaKiran

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

this method looks lengthy  

Valued Guide
Posts: 558

Re: Convert numeric variable into SAS time variable

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
Trusted Advisor
Posts: 1,309

Re: Convert numeric variable into SAS time variable

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;

Super User
Posts: 13,293

Re: Convert numeric variable into SAS time variable

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;
Ask a Question
Discussion stats
  • 5 replies
  • 80 views
  • 0 likes
  • 4 in conversation