I am struggling to combine date and time as a single variable.
data set is like below
Date, time
11May2012 05:11
12May2012 11:54
9:50
13may2012 01:40
15may2012
16may2012
17may2012
18may2012 3:30
out put should be like below
Date, time Date/time
11May2012 05:11 11May2012/05:11
12May2012 11:54 12May2012/11:54
9:50 /09:50
13may2012 01:40 13May2012/01:40
15may2012 15May2012
16may2012 16May2012
17may2012 17May2012
18may2012 3:30 18May2012/03:30
Can anybody help me please
Regards
Forgot about the missing values, so still have to go back to character:
options missing='';
data have;
length datetime $22.;
input date :date9. time: time8.;
if missing(time) then datetime=put(date,date9.);
else datetime=cats(put(date,date9.),'/',put(time,time8.));
format date date9. time time8.;
cards;
11MAY2012 11:50:00
12MAY2012 12:50:00
13MAY2012 .
14MAY2012 14:50:00
15MAY2012 15:50:00
16MAY2012 .
17MAY2012 17:50:00
. 18:50:00
19MAY2012 19:50:00
20MAY2012 20:50:00
;
proc print;run;
Haikuo
Not sure if this is what you want;
data have;
input (Date time) (:$10.);
datetime=ifc(missing(time),date,cats(date,'\',time));
cards;
11May2012 05:11
12May2012 11:54
. 9:50
13may2012 01:40
15may2012 .
16may2012 .
17may2012 .
18may2012 3:30
;
proc print;run;
Haikuo
Hi Hai.kuo
Thank you, but it is throwing as ifc unknown function. Hence didn't work.
datetime=ifc(missing(time),date,cats(date,'\',time));
Oops. I guess your SAS version is earlier than 9.2. IFC() is totally replaceable by using the following:
if missing(time) then datetime=date;
else datatime=cats(data,'\',time);
Regards,
Haikuo
UPdate:
data have;
length datetime $22.;
input (Date time) (:$10.);
if missing(time) then datetime=date;
else datetime=cats(date,'\',time);
cards;
11May2012 05:11
12May2012 11:54
. 9:50
13may2012 01:40
15may2012 .
16may2012 .
17may2012 .
18may2012 3:30
;
Haikuo
Hi Haikuo,
Thanks it worked with the datalines.
I have another questions: I am referring date and time from a SAS data set, if i use the above code it is printing in SAS format. can you help in with the correct code. I am using following code:
data abc;
set project.abc;
length datetime $20.;
if missing(f1) then datetime=f2;
else datetime=cats(f1,'/',f2);
label f1="date" f2="time";
run;
proc print label;
run;
the out is showing like below:
Obs date time datetime
1 11MAY2012 11:50:00 19124/42600
2 12MAY2012 12:50:00 19125/46200
3 13MAY2012 . 19126/.
4 14MAY2012 14:50:00 19127/53400
5 15MAY2012 15:50:00 19128/57000
6 16MAY2012 . 19129/.
7 17MAY2012 17:50:00 19130/64200
8 . 18:50:00 67800
9 19MAY2012 19:50:00 19132/71400
10 20MAY2012 20:50:00 19133/75000
Thank you very much
Best regards
Suresh
Then we have started wrong. If you don't really care about '/', then we can do:
data have;
input date :date9. time: time8.;
datetime=dhms(date,0,0,time);
format date date9. time time8. datetime datetime20.;
cards;
11MAY2012 11:50:00
12MAY2012 12:50:00
13MAY2012 .
14MAY2012 14:50:00
15MAY2012 15:50:00
16MAY2012 .
17MAY2012 17:50:00
. 18:50:00
19MAY2012 19:50:00
20MAY2012 20:50:00
;
proc print;run;
Haikuo
I tried this code before, but here I am missing the date or time if one either one is missing. Is there any better way that I can print together the way I want.
More over I am referring date and time from a SAS data set variables.
I appreciate your time and help.
Regards
Suresh
Forgot about the missing values, so still have to go back to character:
options missing='';
data have;
length datetime $22.;
input date :date9. time: time8.;
if missing(time) then datetime=put(date,date9.);
else datetime=cats(put(date,date9.),'/',put(time,time8.));
format date date9. time time8.;
cards;
11MAY2012 11:50:00
12MAY2012 12:50:00
13MAY2012 .
14MAY2012 14:50:00
15MAY2012 15:50:00
16MAY2012 .
17MAY2012 17:50:00
. 18:50:00
19MAY2012 19:50:00
20MAY2012 20:50:00
;
proc print;run;
Haikuo
Great Haikuo,
It worked. But still have a question. in my data set times are like below
7:50
11:30
8:40
can we make a output like below
07:50
11:30
08:40
This is in addition to your above code.
Regards
Suresh
To replace all of the format 'time8.' with 'time5.' should fix it.
Haikuo
Replaced and tried didnot work.
Can you suggest me how to calculate study_day which I have initiated in another discussion as "Study day calculation problem"
You are Great.
Regards
Suresh
Somehow it works for me:
options missing='';
data have;
length datetime $22.;
input date :date9. time: time5.;
if missing(time) then datetime=put(date,date9.);
else datetime=cats(put(date,date9.),'/',put(time,time5.));
format date date9. time time5.;
cards;
11MAY2012 11:50
12MAY2012 12:50
13MAY2012 .
14MAY2012 14:50
15MAY2012 15:50
16MAY2012 .
17MAY2012 17:50
. 18:50
19MAY2012 19:50
20MAY2012 20:50
;
proc print;run;
Will look for another thread of yours.
Haikuo
That will require storing the data as a character field and Haikuo solution will work.
But that makes it harder to process the info in the future.
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.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.