BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Xing
Fluorite | Level 6
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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

4 REPLIES 4
derekg
Obsidian | Level 7

 

data test2;
	set test;
	where '01jan2012'd <= admdate <= '01jan2013'd;
run;

 

ballardw
Super User

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.

Xing
Fluorite | Level 6

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?

ballardw
Super User

@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.

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
  • 4 replies
  • 5467 views
  • 1 like
  • 3 in conversation