Programming the statistical procedures from SAS

Check date range from a datasets with 2 dates

Reply
N/A
Posts: 0

Check date range from a datasets with 2 dates

hi,

I have a problem on checking date range, consider below dataset layout

N Date1 Date2 Value
1 21Jan2009 22Feb2009 5
2 21Jan2009 22Mar2009 6
3 21May2009 22Nov2009 9
4 21Oct2009 22Oct2009 34
5 21Jan2009 22June2009 6

I want to select entries with the dates fall in October 2009
(ie, outcome is record #3,#4 is being selected)

any efficient way to compile this??

Many thanks
SAS Super FREQ
Posts: 8,683

Re: Check date range from a datasets with 2 dates

Hi:
I'm not sure what you mean by selecting entries...do you want a report (such as from PROC PRINT) or a dataset (such as from PROC SQL)???

If all you want is a report, you could use a WHERE clause in PROC PRINT. Something like this (assumes that date1 and date2 are numeric variables in the SAS dataset):
[pre]
proc print data=lib.data;
title 'Use WHERE clause for selection';
where 10 between month(date1) and month(date2) and
(year(date1)=2009 or year(date2)=2009);
format date1 date2 mmddyy10.;
run;
[/pre]

If the WHERE statement selects the observations you want in the PROC PRINT, then you could use the same WHERE logic in a PROC SQL query to create a dataset. If you know your data are all from 2009, then you don't need the check for year.

cynthia
N/A
Posts: 0

Re: Check date range from a datasets with 2 dates

thx cythnia!!

actually, proc print or data selection is okay, the dataset may be like this

N Date1 Date2 Value
1 21Jan2009 22Feb2009 5
2 21Jan2009 22Mar2009 6
3 21May2009 22Nov2009 9 *
4 21Oct2009 22Oct2009 34 *
5 21Jan2009 22June2009 6
6 3 Mar 2005 22Oct2009 3 *
7 3 Mar 2007 31Dec2999 2 *

then i want to select records with any dates in October 2009 fall into Date 1 and Date2
after i rethink about this scenario, i think

if date1 <= '31oct2009'd and date2 >= '1oct2009'd

may solve the problem

btw, any sas function can cater this scenairo?

Thanks again
Super Contributor
Super Contributor
Posts: 3,174

Re: Check date range from a datasets with 2 dates

The SAS function INTNX can be used to test a SAS variable or constant against a SAS literal (or another SAS variable) as you might need -- using the test you performed for '01OCT2009'd. Check the SAS DOC on this function - also there will be sample code content and technical/conference papers using the function on the SAS.COM website.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 3 replies
  • 83 views
  • 0 likes
  • 3 in conversation