data test;
input admdate : mmddyy10. id @@;
format admdate mmddyy10.;
cards;
05/01/2009 1
08/01/2010 2
07/01/2012 3
08/24/2007 4
12/11/2012 5
;
run;
Currently, I have a dataset with date and id. I want to select all the observations that the date is within 1 year prior to 1/1/2013. That is I want to select all the obs with the date between 1/1/2012 and 1/1/2013. What should I do?
Thanks!
Here are three ways that may work. They may need adjusting a little depending on whether the dates you mentioned as boundaries should be included or not.
data want; set test; /* between to specified dates*/ where '01JAN2012'd le admdate lt '01JAN2013'd; run; data want2; set test; /* within a specified year*/ where year(admdate) = 2012; run; data want3; set test; /* "year" as in from date*/ where intck('year',admdate,'01JAN2013'd,'c') = 0; run;
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
Important: Any time you want to use a literal data is has to be in either DATE9. or DATE7. format, in quotes, with the D immediately following to tell SAS the value is a date literal value.
data test2;
set test;
where '01jan2012'd <= admdate <= '01jan2013'd;
run;
Here are three ways that may work. They may need adjusting a little depending on whether the dates you mentioned as boundaries should be included or not.
data want; set test; /* between to specified dates*/ where '01JAN2012'd le admdate lt '01JAN2013'd; run; data want2; set test; /* within a specified year*/ where year(admdate) = 2012; run; data want3; set test; /* "year" as in from date*/ where intck('year',admdate,'01JAN2013'd,'c') = 0; run;
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
Important: Any time you want to use a literal data is has to be in either DATE9. or DATE7. format, in quotes, with the D immediately following to tell SAS the value is a date literal value.
if the date is stored in a variable and I want to see 1 year prior to each date in this variable, what should I do?
@Xing wrote:
if the date is stored in a variable and I want to see 1 year prior to each date in this variable, what should I do?
Are you asking "Given a specific date such as 21JUN2019, how do I get a SAS date value that is the same date one year ago?".
newdate = intnx('year',date,-1,'same');
if that is not what you want you need to provide a much more concrete example such as with actual dates and the desired result.
Really should look at this: https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/... has a PDF with much information about dates.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.