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 ![]()
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;
newdate=datepart(input(olddate,datetime18.));
ie:
data _null_;
olddate = "13may2005:00:00:00";
format newdate date9.;
newdate=datepart(input(olddate,datetime18.));
put newdate=;
run;
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!! ![]()
thanks for your support!!
koushik.
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);
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.
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
;
Slick!
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;
hello,
wow..... really thanks a lot for your support... this worked like a magic..................this is very simple and easy....
you rockkk!!!!! ![]()
hi all,
thanks for investing your time... you guys are very helpful!!!!
thanks a lot to all............ ![]()
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.