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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 438 views
  • 0 likes
  • 2 in conversation