Help using Base SAS procedures

Concat Date and Time

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Concat Date and Time

Hi, I need to concat Date and Time using proc sql

Date  

15/05/2018

Time

8:27:30

DateTime

15/05/2018 08:27:30

 

So I did that: Smiley Happy

 

cat(put(day(Date),z2.),'/',put(month(Date),z2.),'/',year(Date),' ',put(hour(Time),z2.),':',put(minute(Time),z2.),":",put(second(Time),z2.)) as DateTime

 

Anyone can show me another way please one that I don’t mess with the column format


Accepted Solutions
Solution
‎05-30-2018 02:16 PM
Super User
Posts: 23,700

Re: Concat Date and Time


All Replies
Solution
‎05-30-2018 02:16 PM
Super User
Posts: 23,700

Re: Concat Date and Time

 

new_var = dhms(date, 0, 0, time);

format new_var datetime.;

This may be helpful.

https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

New Contributor
Posts: 4

Re: Concat Date and Time

Ok it works, but i need this format 15/05/18:08:27:30 and datetime. returns 15MAY18:08:27:30.
Super User
Posts: 13,523

Re: Concat Date and Time


@godoyau wrote:
Ok it works, but i need this format 15/05/18:08:27:30 and datetime. returns 15MAY18:08:27:30.

The value appears correct, what you are actually saying is that the display format does not meet you expectation/ need.

SAS provides a very large number of date, time and datetime display formats but cannot meet every single need. However you can create custom display format using Proc format with the Picture statement and directives. Warning for the code below: The quotes around the funny list of %0d/%0m/0%y:%0H:%0M:%0S must be single quotes or else SAS will think you are using macro variables. The case on the d m y H M and S are significant. Changing from 'm' to 'M' for instance will change the displayed value from month number to Minutes (and vice versa). The 0 are to display leading zeroes for values less than 10. Since this is not SAS supplied format it will be up to you to make sure the format is available when ever you need to use it. Either rerun the code every session or add the format to permanent format catalog.

 

proc format library=work;
picture mydttime
low - high= '%0d/%0m/0%y:%0H:%0M:%0S' (datatype=datetime)
;
run; 

data junk;
   x='15MAY18:08:27:30'dt;
   put x= mydttime.;
run;
PROC Star
Posts: 1,784

Re: Concat Date and Time

Are you dates and time char or numeric?

New Contributor
Posts: 4

Re: Concat Date and Time

Posted in reply to novinosrin

Numeric

☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 165 views
  • 0 likes
  • 4 in conversation