BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Almighty
Fluorite | Level 6

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

1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

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

View solution in original post

13 REPLIES 13
Haikuo
Onyx | Level 15

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

Almighty
Fluorite | Level 6

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));

Haikuo
Onyx | Level 15

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

Haikuo
Onyx | Level 15

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

Almighty
Fluorite | Level 6

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

Haikuo
Onyx | Level 15

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

Almighty
Fluorite | Level 6

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

Haikuo
Onyx | Level 15

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

Almighty
Fluorite | Level 6

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

Haikuo
Onyx | Level 15

To replace all of the format 'time8.' with 'time5.' should fix it.

Haikuo

Almighty
Fluorite | Level 6

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

Haikuo
Onyx | Level 15

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

Reeza
Super User

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.

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!

What is Bayesian Analysis?

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.

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
  • 13 replies
  • 10411 views
  • 0 likes
  • 3 in conversation