WHERE statement for dates?

Accepted Solution Solved
Reply
Contributor
Posts: 41
Accepted Solution

WHERE statement for dates?

Hello,

I am looking for information on how to use a WHERE statement in a PROC PRINT to specify a date. For example I want to show all observations where data equal to 1 Jan 2000.

Regards,

P.


Accepted Solutions
Solution
‎12-18-2012 03:57 PM
Super Contributor
Posts: 543

Re: WHERE statement for dates?

hi,

proc print data = your_data;

     where date = mdy(01,01,2000);

var var_list;

run;

Best of luck,

Anca.

View solution in original post


All Replies
Solution
‎12-18-2012 03:57 PM
Super Contributor
Posts: 543

Re: WHERE statement for dates?

hi,

proc print data = your_data;

     where date = mdy(01,01,2000);

var var_list;

run;

Best of luck,

Anca.

Contributor
Posts: 41

Re: WHERE statement for dates?

Hi Anca,

Thanks for that. Is it possible to use the same function to return all observations from January 2000, regardless of the day?

Regards,

P.

Super Contributor
Posts: 1,636

Re: WHERE statement for dates?

print data=have;

where date between mdy(1,1,2000) and mdy(1,31,2000);

run;

Super Contributor
Posts: 1,636

Re: WHERE statement for dates?

or:

data have;

input date mmddyy10.;

format date mmddyy10.;

cards;

01/31/2000

01/02/2000

02/04/2000

;

proc print data=have;

where date between intnx('month','01jan2000'd,0,'b') and intnx('month','01jan2000'd,0,'e') ;

run;

Super User
Posts: 17,819

Re: WHERE statement for dates?

where date = '01jan2000'd;

Notice the d at the end and the quotations around the date. The quotations can be single or double and the year can be two or four digit.

Contributor
Posts: 41

Re: WHERE statement for dates?

Hi Reeza,

What is the d at the end for? Also how would SAS know the date format (e.g.: dd/mm/yy, mm/dd/yy, dd/mm/yyyy, etc) and would it be possible to specify only the month and the year?

Regards,

P.

Super User
Posts: 17,819

Re: WHERE statement for dates?

SAS stores dates as numbers, the format is only used to display the numbers so when you specify the 'd' it tells SAS it's a date and it converts it into a number.

You can also specify a between with the literals:

where date between '01jan2000'd and '31jan2000'd but then you have to know the start and end of the month. 

LinLin's solution above is better for that.

You could also specify the month and year separately

where month(date)=1 and year(date)=2000;

Super Contributor
Posts: 644

Re: WHERE statement for dates?

@pmdci

The SAS date literal requires the date to be in a DATE informat.  Other date informats are not supported. The following will work (not an exhaustive list):

     '01jan80'd

     '01jan1980'd

     '01-jan-1980'd

You could use the INTCK function :

print data=have;

     where intck('month', '1jan2000'd, date) = 0 ;

run;


Richard

Respected Advisor
Posts: 3,124

Re: WHERE statement for dates?

As you can see, SAS is very resourceful when dealing with date variable, the following two options should work for you as well:

where month(date)=1 and year(date)=2000;

where put(date,monyy7.)='JAN2000';

Haikuo

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 9 replies
  • 392 views
  • 6 likes
  • 6 in conversation