DATA Step, Macro, Functions and more

extracting the date using the datepart function??

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

extracting the date using the datepart function??

hello community,

in the question data set i have date in as "13may2005:00:00:00. It is in the character type. could some one help me how do i extract the date from this using the datepart?? i am new to sas.

thanks for your help Smiley Happy


Accepted Solutions
Solution
‎10-30-2014 12:16 PM
Occasional Contributor
Posts: 10

Re: extracting the date using the datepart function??

It seems you should be most if not all the way there.

For me there are two main ways you can use with the date being in a string, firstly input and date part, and secndly substring and input.

Here is some code demonstrating what i mean.

data source_ds;

input race_date $20.;

datalines;

12may2005:00:00:00

04sep2006:00:00:00

27jan2005:00:00:00

;

run;

data target_ds;

set source_ds;

format new_race_date date9.;

/* date part method */

new_race_date=datepart(input(race_date,datetime18.));

/* substring method */

new_race_date=input(substr(race_date,1,9),date9.);

run;

View solution in original post


All Replies
Super User
Posts: 7,831

Re: extracting the date using the datepart function??

newdate=datepart(input(olddate,datetime18.));

ie:

data _null_;

olddate = "13may2005:00:00:00";

format newdate date9.;

newdate=datepart(input(olddate,datetime18.));

put newdate=;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: extracting the date using the datepart function??

Posted in reply to KurtBremser

hello,

thanks for the replay. In my question i have multiple rows.it looks like this

race_date

12may2005:00:00:00

04sep2006:00:00:00

27jan2005:00:00:00

and all i need is to create a new column(new_race_date) in which the date from the column(race_date) is extracted!! Smiley Happy

thanks for your support!!

koushik.

Super User
Super User
Posts: 7,977

Re: extracting the date using the datepart function??

So race_date is a character yes?  And you want new_race_date to also be a character yes?

If so then:

data xyx;

     set xyx;

     new_race_date=scan(race_date,1,':');

run;

If you want new_race_date to be numeric then:

     new_race_date=input(scan(race_date,1,':'),date9.);

If race_date is already numeric and you want numeric then:

     new_race_date=datepart(race_date);

And for numeric race_date to character new_race_date:

     new_race_date=put(datepart(race_date);

Super User
Posts: 7,831

Re: extracting the date using the datepart function??

You just need to insert your variable names in my example. Can't be that complicated, no?

If it does not work, post the log.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Trusted Advisor
Posts: 1,228

Re: extracting the date using the datepart function??

data have;

input race_date $20.;

format new_race_date date9.;

new_race_date=input(race_date,date9.);

datalines;

12may2005:00:00:00

04sep2006:00:00:00

27jan2005:00:00:00

;

Respected Advisor
Posts: 3,156

Re: extracting the date using the datepart function??

Slick!

Solution
‎10-30-2014 12:16 PM
Occasional Contributor
Posts: 10

Re: extracting the date using the datepart function??

It seems you should be most if not all the way there.

For me there are two main ways you can use with the date being in a string, firstly input and date part, and secndly substring and input.

Here is some code demonstrating what i mean.

data source_ds;

input race_date $20.;

datalines;

12may2005:00:00:00

04sep2006:00:00:00

27jan2005:00:00:00

;

run;

data target_ds;

set source_ds;

format new_race_date date9.;

/* date part method */

new_race_date=datepart(input(race_date,datetime18.));

/* substring method */

new_race_date=input(substr(race_date,1,9),date9.);

run;

Occasional Contributor
Posts: 13

Re: extracting the date using the datepart function??

hello,

wow..... really thanks a lot for your support... this worked like a magic..................this is very simple and easy....

you rockkk!!!!! Smiley Happy

Occasional Contributor
Posts: 13

Re: extracting the date using the datepart function??

hi all,

thanks for investing your time... you guys are very helpful!!!!

thanks a lot to all............  Smiley Happy

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 2562 views
  • 8 likes
  • 6 in conversation