BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
wlierman
Lapis Lazuli | Level 10

This is a pretty basic question that nonetheless keeps tripping me up.

 

I have a datetimevariable.  I extract the datepart and it is returned as a SAS date form.

 

I want a a DatePart form  like  dd/mm/yy. Here is the code

Data SASCDC_2.Arias_Median_Days_Monitoring;
   Set SASCDC_2.Arias_Median_Days_Monitoring;
   C_Date=DatePart(Created_On);
   Create = ??? (C_Date);
   rename Create = Create_date;
run;

I put the ? marks in there.

 

So I have the following output

 

                Created_on                        C_date

             26JUN20:09:40:24                22092

 

So I want Create (in code) to contain the DatePart representation of C_date

like  26JUN20  and so on for the rest.

 

Thank you for your help.

 

wlierman

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

You've done it correctly but have not formatted it. Computer applications often store dates as the number of days from a reference point so 22092 is the number of days from January 1, 1960. To have it show as a date you apply a format.

 

FYI I strongly recommend against coding so that your input data set has the same name as the output data set (DATA/SET statements). This makes it hard to debug your code and is not a good practice.

 

Data SASCDC_2.Arias_Median_Days_Monitoring2;
   Set SASCDC_2.Arias_Median_Days_Monitoring;

   C_Date=DatePart(Created_On);
   format c_date ddmmyyd10.;

run;

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 


@wlierman wrote:

This is a pretty basic question that nonetheless keeps tripping me up.

 

I have a datetimevariable.  I extract the datepart and it is returned as a SAS date form.

 

I want a a DatePart form  like  dd/mm/yy. Here is the code

Data SASCDC_2.Arias_Median_Days_Monitoring;
   Set SASCDC_2.Arias_Median_Days_Monitoring;
   C_Date=DatePart(Created_On);
   Create = ??? (C_Date);
   rename Create = Create_date;
run;

I put the ? marks in there.

 

So I have the following output

 

                Created_on                        C_date

             26JUN20:09:40:24                22092

 

So I want Create (in code) to contain the DatePart representation of C_date

like  26JUN20  and so on for the rest.

 

Thank you for your help.

 

wlierman


 

View solution in original post

2 REPLIES 2
Reeza
Super User

You've done it correctly but have not formatted it. Computer applications often store dates as the number of days from a reference point so 22092 is the number of days from January 1, 1960. To have it show as a date you apply a format.

 

FYI I strongly recommend against coding so that your input data set has the same name as the output data set (DATA/SET statements). This makes it hard to debug your code and is not a good practice.

 

Data SASCDC_2.Arias_Median_Days_Monitoring2;
   Set SASCDC_2.Arias_Median_Days_Monitoring;

   C_Date=DatePart(Created_On);
   format c_date ddmmyyd10.;

run;

Here's a great, but longer and in depth, reference for dates and times in SAS
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 


@wlierman wrote:

This is a pretty basic question that nonetheless keeps tripping me up.

 

I have a datetimevariable.  I extract the datepart and it is returned as a SAS date form.

 

I want a a DatePart form  like  dd/mm/yy. Here is the code

Data SASCDC_2.Arias_Median_Days_Monitoring;
   Set SASCDC_2.Arias_Median_Days_Monitoring;
   C_Date=DatePart(Created_On);
   Create = ??? (C_Date);
   rename Create = Create_date;
run;

I put the ? marks in there.

 

So I have the following output

 

                Created_on                        C_date

             26JUN20:09:40:24                22092

 

So I want Create (in code) to contain the DatePart representation of C_date

like  26JUN20  and so on for the rest.

 

Thank you for your help.

 

wlierman


 

wlierman
Lapis Lazuli | Level 10

Thank you for your review and solution.

 

Also thank you for the coding tips as well as well as the link to Andrew Karp's article.

 

Thank you.

 

wlierman

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 2 replies
  • 764 views
  • 0 likes
  • 2 in conversation