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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Malv72
Calcite | Level 5

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

9 REPLIES 9
Kurt_Bremser
Super User

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

ie:

data _null_;

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

format newdate date9.;

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

put newdate=;

run;

koushik
Calcite | Level 5

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

stat_sas
Ammonite | Level 13

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

;

Malv72
Calcite | Level 5

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;

koushik
Calcite | Level 5

hello,

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

you rockkk!!!!! Smiley Happy

koushik
Calcite | Level 5

hi all,

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

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

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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