Extracting date, month, year and time from a datetime variable

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 145
Accepted Solution

Extracting date, month, year and time from a datetime variable

[ Edited ]

Hi Everyone,

 

Can some one help me with extracting DAY, MONTH, YEAR and Time from the below format....

 

2015-08-27T 8:36:00

2014-09-30T14:45:00

 

The output should look like this…

       STDTC                           ST_Y ST_M ST_D ST_TM

2015-08-27T 8:36:00       2015   8   27   08:36

2014-09-30T14:45:00      2014  9    30   14:45

 

Thanks in advance

 

Rakesh


Accepted Solutions
Solution
‎02-25-2016 04:54 PM
Super User
Posts: 17,819

Re: Extracting date, month, year and time from a datetime variable

1. Datepart() function to extract date

2. Timepart() function to extract time

3. Year(), Month(), Day() function to extract date components - reference variable from #1

View solution in original post


All Replies
Solution
‎02-25-2016 04:54 PM
Super User
Posts: 17,819

Re: Extracting date, month, year and time from a datetime variable

1. Datepart() function to extract date

2. Timepart() function to extract time

3. Year(), Month(), Day() function to extract date components - reference variable from #1

Regular Contributor
Posts: 212

Re: Extracting date, month, year and time from a datetime variable

Hi mate,

 

Try this. Not the best option but works:

 

data dt;
  date="2015-08-27T 8:36:00 ";
run;

data dt2;
   set dt;
   ST_Y  = input(scan(date,1,"-"),Year.);
   ST_M  = input(scan(date,2,"-"),month.);
   ST_D  = input(scan(scan(date,1,"T"),3,"-"),day.);
   ST_TM = input(scan(date,2,"T"),nltime10.);
   format ST_TM nltime10.;
run;

Hope this helps

☑ This topic is SOLVED.

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

Discussion stats
  • 2 replies
  • 915 views
  • 1 like
  • 3 in conversation