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
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
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
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.