SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
librasonali
Quartz | Level 8
data housekeeping_Data;
input Date DATE10. ;
format Date DATE10.;
cards;
26DEC2020
25DEC2020
27DEC2020
28DEC2020
29DEC2020
30DEC2020
17NOV2020
15NOV2020
01NOV2020
11NOV2020
;
run;

Hi everyone , 

 

Above is the input data now I want that the observations which are 5 days old from my Today()  should be deleted/removed whenever I run the date.

FYI : I used,  five_mo_ago = intnx('date',today(), -5 ,same);  but It is not working. 

Any other approach is welcomed. 

output should be like below when today is 27th Dec'2020:

 

Obs Date
1 26-Dec-20
2 25-Dec-20
3 27-Dec-20
4 28-Dec-20
5 29-Dec-20
6 30-Dec-20
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Why did you calculate a date that is five days before TODAY but then not use it.

Note that SAS stores dates as number of days.  So to get a difference in days just subtract two date values.

If you have a date, X, and another date, Y then the difference in days is X-Y.   If the difference is negative then Y is after X.  If the difference is positive then X is after Y.

 

So if today is 15DEC2020 then 5 day ago would be 10DEC2020, which you can calculate by using (today()-5).  If you want to remove values with dates before 10DEC2020 then code something like:

if date < (today()-5) then delete;

If you also want to delete the dates that fall on 10DEC2020 then use <= instead of = in the comparison.

What do you want to do with observations where DATE is missing?  SAS will treat missing values as less than any actual number so observations with missing value of DATE will be deleted by the statement above.

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You can try

 

five_mo_ago = intnx('day',today(), -5);

 

Other than that, I'm afraid I don't understand your problem. Do you mean you want to delete records where variable DATE is greater than 5 days old? Or do you mean something else?

 

Please don't ever say "but It is not working" and provide no other information. We can't help you if you provide no other information. Please do show us the LOG for the code that isn't working (all of it, 100%, the code as it appears in the LOG, the ERRORs, WARNINGs, NOTEs, in the sequence it appears in the log, with nothing chopped out).

 

--
Paige Miller
librasonali
Quartz | Level 8

@PaigeMiller wrote:

You can try

 

five_mo_ago = intnx('day',today(), -5);

 

Other than that, I'm afraid I don't understand your problem. Do you mean you want to delete records where variable DATE is greater than 5 days old? Or do you mean something else?

 

Please don't ever say "but It is not working" and provide no other information. We can't help you if you provide no other information. Please do show us the LOG for the code that isn't working (all of it, 100%, the code as it appears in the LOG, the ERRORs, WARNINGs, NOTEs, in the sequence it appears in the log, with nothing chopped out).

 


I did not pasted because was not able to code much on this but still I am keeping my code below.  :

data final;
set housekeeping_data;
five_days_ago = intnx('day',today(), -5);
if date < Today() then delete /* HERE  want that the observations which are 5 days old from my Today() 
should be deleted/removed*/;

drop five_days_Ago;
proc print;

run;

 

Yes, Do you mean you want to delete records where variable DATE is gt than 5 days old from today.

PaigeMiller
Diamond | Level 26
if date < five_days_ago then delete;
--
Paige Miller
Tom
Super User Tom
Super User

Why did you calculate a date that is five days before TODAY but then not use it.

Note that SAS stores dates as number of days.  So to get a difference in days just subtract two date values.

If you have a date, X, and another date, Y then the difference in days is X-Y.   If the difference is negative then Y is after X.  If the difference is positive then X is after Y.

 

So if today is 15DEC2020 then 5 day ago would be 10DEC2020, which you can calculate by using (today()-5).  If you want to remove values with dates before 10DEC2020 then code something like:

if date < (today()-5) then delete;

If you also want to delete the dates that fall on 10DEC2020 then use <= instead of = in the comparison.

What do you want to do with observations where DATE is missing?  SAS will treat missing values as less than any actual number so observations with missing value of DATE will be deleted by the statement above.

 

librasonali
Quartz | Level 8
yes , i should think that subtracting -5 , date difference also I used initially but

if date < (today()-5) then delete; this worked. Thanks

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 7728 views
  • 1 like
  • 3 in conversation