BookmarkSubscribeRSS Feed
Specter
Calcite | Level 5

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.

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

Something like this?

 

data want;
	set car;
	where datepart(date) > today()-7;
run;
Ksharp
Super User

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

%put &x ;

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


ShiroAmada
Lapis Lazuli | Level 10

/* 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;
 
 
     
ballardw
Super User

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.

Reeza
Super User

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.


 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 5 replies
  • 3760 views
  • 2 likes
  • 6 in conversation