DATA Step, Macro, Functions and more

Select Dataset older than 7 days

Reply
New Contributor
Posts: 2

Select Dataset older than 7 days

Hello,

 

i have a dataset with column date for example 10SEP2017:12:05:00. Now i would like to use where function to filter the date older then 7 days.

 

My Code:

 

data car;

dateold=today()-7;

format dateold DATE9.;

set work.car;

/* this part is working */

where date = '10SEP2017:0:0:0'dt;

/* this not */

where date = 'dateold:0:0:0'dt;

 run;

 

Can somebody provide the SAS code inorder to do the above logic.

Thank you.

PROC Star
Posts: 1,400

Re: Select Dataset older than 7 days

Something like this?

 

data want;
	set car;
	where datepart(date) > today()-7;
run;
Super User
Posts: 10,850

Re: Select Dataset older than 7 days


%let x=%sysfunc(intnx(dtday,%sysfunc(datetime()),-7,s),datetime20.);

%put &x ;

data x;
 set have;
 if column < "&x"dt;
run;


Frequent Contributor
Posts: 118

Re: Select Dataset older than 7 days

[ Edited ]

/* Approach #1 */


data car; dateold=today()-7; format dateold DATE9.; set work.car; /* this part is working */ where date = '10SEP2017:0:0:0'dt; /* this not */ where datepart(date) > dateold;
run;




/* # 2 */
data car; set work.car; /* this part is working */ where date = '10SEP2017:0:0:0'dt; /* this not */ where datepart(date) > today()-7;
run;

 
/* Approach # 3 */

data car; set work.car; where date = '10SEP2017:0:0:0'dt AND datepart(date) > today()-7;
run;
 
 
     
Super User
Posts: 13,941

Re: Select Dataset older than 7 days

I suggest that when working with SAS variables to be very careful about distinguishing between Date variables and Datetime variables. Without an example value (thank you for including one) you would likely receive suggestions that will not work because SAS Date values and Datetime values measure different things, dates are days and datetime is seconds.

 

Also if your datetime values all have the excact same time component then I suggest ditching the time part and actually use the date component only.

Super User
Posts: 24,010

Re: Select Dataset older than 7 days

You have a DATETIME variable. 

Use the DATEPART function to obtain just the date part.

Or use INTNX to increment the datetime variable 7 days earlier.

 

The TODAY or DATE() function will return todays date, -7 will subtract 7 from the date and this will filter out dates less than 7 days. You may want to check the boundaries here, ie should it be < or <=  or do you really mean today() - 6?

 

Anyways, this should get you started.

 

where datepart(date) < today()-7;

Specter wrote:

Hello,

 

i have a dataset with column date for example 10SEP2017:12:05:00. Now i would like to use where function to filter the date older then 7 days.

 

My Code:

 

data car;

dateold=today()-7;

format dateold DATE9.;

set work.car;

/* this part is working */

where date = '10SEP2017:0:0:0'dt;

/* this not */

where date = 'dateold:0:0:0'dt;

 run;

 

Can somebody provide the SAS code inorder to do the above logic.

Thank you.


 

Ask a Question
Discussion stats
  • 5 replies
  • 164 views
  • 2 likes
  • 6 in conversation