Export date yyyy.mm.dd and time hh:mm

Accepted Solution Solved
Reply
Super Contributor
Posts: 417
Accepted Solution

Export date yyyy.mm.dd and time hh:mm

Hello Everyone,

I have a dataset of Date and Time as below and I want to export it to text file with the format

yyyy.mm.dd hh:mm (once space between date and time)

Date and Time in SAS is always a nightmare to me and I very much appreciate it if you could help me with this problem.

Many thanks,

HHC

data have;

input date time;

informat time time11.;

format date date9. time time11.;

datalines;

20130730 4:00:00

20130130 5:15:00

20120515 15:45:00

;

Output file

2013.07.30  04:00

2013.01.30  05:15

2012.05.15  15:45


Accepted Solutions
Solution
‎10-24-2013 05:13 AM
SAS Super FREQ
Posts: 708

Re: Export date yyyy.mm.dd and time hh:mm

Hi

You can create your own datetime format to get the date in format you like. Also have a look at the DHMS function it provides a simple way to create a datetime value from a date and a time value.

See code below:

proc format;
 
picture myDT (default=16)
    low - high =
'%Y.%0m.%0d %0H:%0M' (datatype=datetime)
  ;
run;

data have;
  input
    date :
yymmdd8.
    time :
time8.
  ;
  datetime = dhms(date, 0, 0, time);

 
format
    date
date9.
    time
tod8.
    dateTime
myDT.
  ;
  datalines;
20130730 4:00:00
20130130 5:15:00
20120515 15:45:00
;

Bruno

View solution in original post


All Replies
Respected Advisor
Posts: 3,156

Re: Export date yyyy.mm.dd and time hh:mm

data a;

input date :yymmdd10. time :time11.;

format date date9. time time11.;

out=catx(' ',put(date,yymmddp10.),put(time,hhmm.));

put out=;

datalines;

20130730 4:00:00

20130130 5:15:00

20120515 15:45:00

;

updated.

Super Contributor
Posts: 417

Re: Export date yyyy.mm.dd and time hh:mm

Thank you, Hai.Kuo,

I really need the 0 in front of hour it is single digit like 04:20 instead of 4:20.

Can you help me to make it?

HHC

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Export date yyyy.mm.dd and time hh:mm

Hi HHC

I tried the following code. I am getting 04 instead of 4 but there is some problem may ne you can fix it.

data a;

input date :yymmdd10. time :time11.;
format date date8. time time11.;
format datetm datetime16.;
datetm=input(put(date, date7.)||''||put(time, time8.), datetime16.);
put datetm=;
datalines;

20130730 4:00:00
20130130 5:15:00
20120515 15:45:00
;
run;


date


time


datetm


30Jul2013


4:00:00


30Jul2013 4:00:00


30Jan2013


5:15:00


30Jan2013 5:15:00


15May2012


15:45:00


15May2012 15:45:00

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Export date yyyy.mm.dd and time hh:mm


Hi

Sorry my SAS data shows a 0 but the above result is Excel which doesn'tshow the 0.

Mit

Frequent Contributor
Frequent Contributor
Posts: 83

Re: Export date yyyy.mm.dd and time hh:mm

datetimedatetm
30-Jul-134:00:0030JUL13:04:00:00
30-Jan-135:15:0030JAN13:05:15:00
15-May-1215:45:0015MAY12:15:45:00
Trusted Advisor
Posts: 1,137

Re: Export date yyyy.mm.dd and time hh:mm

You can use the proc format to get the desired output, something like below

proc format ;

picture tim other = '%0H:%0M' (datatype=time) ;

run ;

  

data a;

input date :yymmdd10. time :time8.;

out=catx(' ',put(date,yymmddp10.),put(time,tim.));

put out=;

datalines;

20130730 4:00:00

20130130 5:15:00

20120515 15:45:00

;

Thanks,

Jagadish

Thanks,
Jag
Solution
‎10-24-2013 05:13 AM
SAS Super FREQ
Posts: 708

Re: Export date yyyy.mm.dd and time hh:mm

Hi

You can create your own datetime format to get the date in format you like. Also have a look at the DHMS function it provides a simple way to create a datetime value from a date and a time value.

See code below:

proc format;
 
picture myDT (default=16)
    low - high =
'%Y.%0m.%0d %0H:%0M' (datatype=datetime)
  ;
run;

data have;
  input
    date :
yymmdd8.
    time :
time8.
  ;
  datetime = dhms(date, 0, 0, time);

 
format
    date
date9.
    time
tod8.
    dateTime
myDT.
  ;
  datalines;
20130730 4:00:00
20130130 5:15:00
20120515 15:45:00
;

Bruno

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 1600 views
  • 6 likes
  • 5 in conversation