BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Geo-
Quartz | Level 8

data want;

set original;

date1 = darepart(date);

run;

 

my date column looks like 01AUG18:18:47:42

and date1 column is changed into 21397 with numeric type.

 

how colud I extract date into 01AUG18 with date type?

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20
data want;
set original;
date1 = datepart(date);
format date1 date9.;
run;

Add date9. format to your date1 variable. Remember, dates are just formatted numbers in SAS. A Date in SAS represents the number of days since 01Jan1960. 

 

 

View solution in original post

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20
data want;
set original;
date1 = datepart(date);
format date1 date9.;
run;

Add date9. format to your date1 variable. Remember, dates are just formatted numbers in SAS. A Date in SAS represents the number of days since 01Jan1960. 

 

 

Jeetesh
Calcite | Level 5

Hi All,

 

Can any one help me with Extracting only date part from datetime with full program.

PaigeMiller
Diamond | Level 26

Does not the above solution to the problem meet your needs? If not explain in detail.

--
Paige Miller
lih
Calcite | Level 5 lih
Calcite | Level 5

I have same problem. My date variable is 01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution? 

Thanks!

ballardw
Super User

@lih wrote:

I have same problem. My date variable is 01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution? 

Thanks!


Run Proc Contents on your data set. Show us the result. Make sure do indicate which variable is the one in question.

If you don't know how  to run proc contents it is very simple, place the name of your data set after the data= in the example code below:

proc contents data=yourdatasetname;
run;
Tom
Super User Tom
Super User

@lih wrote:

I have same problem. My date variable is 01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution? 

Thanks!


For the DATEPART() function to work the variable needs to NUMERIC with times since 1960 in seconds.

For the SUBSTR() function to work the value needs to be CHARACTER.

 

You cannot use them BOTH on the same variable.

 

If you have a variable named DATETIME that has DATETIME values then you can make DATE, YEAR, MONTH and DAY variables like this:

date=datepart(datetime);
year=year(date);
month=month(date);
day=day(month);
PaigeMiller
Diamond | Level 26

@lih wrote:

I have same problem. My date variable is 01JAN1960:01:47:52 (datetime20.). I tried datepart(date) and read day,month,year using substr, both not work, not sure why. Anybody has solution? 


Important point: if something does not work, we don't know what you did. You need to show us your code, if there are errors in the log, show us the entire log for this DATA step; if it works but gives an answer you think is wrong, show us your code and show the wrong answer.

 

Wild guess:

 

You did not assign a format to the variable created by datepart(date), and it returns an answer of zero, which is the correct answer. You need to assign a DATE9. format to this variable to show the dates in "human readable" form which is 01JAN1960.

 

If that's not it, provide the information I asked for.

--
Paige Miller
DrAbhijeetSafai
Lapis Lazuli | Level 10

Hi @Jeetesh PeterClemmensen has given the full program which is extracting only date part. 

 

I am copy pasting his programme below and I will share what each step means.

 

data want;

/*Here you are creating the dataset which you want to create*/
     set original;

/*Here you are reading the data which has this date variable*/
     date1 = datepart(date);

/*Here you are extracting date part of the date in a variable named date1*/
     format date1 date9.;

/*However, it will be sas date value, so to bring it in human readable form this date9 format it used*/
run;

 

Feel free to let us know if anything of this is not understood.

 

Thank you.

 

- Dr. Abhijeet Safai

 

 

Dr. Abhijeet Safai
Certified Base and Clinical SAS Programmer
Associate Data Analyst
Actu-Real

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 8 replies
  • 77653 views
  • 10 likes
  • 8 in conversation