Home > Mobile >  how do i write in sas a filter that subtracts dates
how do i write in sas a filter that subtracts dates

Time:05-04

Hello Stackoverflow community... hope you can help with this sas question.

I need to create a filter for a table which gives back only those records that are active from last year forward.

I would like to obtain something like :

data want;
set have;
where expire_date >= current(date) - 1year:
run;

the format of the expire_date column is 03MAY2022 (date9. format)... I tried to transform the date into a number and then subtracting 365, but i guess there is a better solution.

can someone illuminate me?

thanks in advance

CodePudding user response:

I think you are searching for the INTNX() function: https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p10v3sa3i4kfxfn1sovhi5xzxh8n.htm#n1lchasgjah7ran0z2wlmsbfwdx2 For example:

data a;
  format num_date num_date_minus_year DATE9.;
  char_date="03MAY2022";
  num_date = inputn (char_date, "DATE9.");
  num_date_minus_year = intnx ('YEAR', num_date, -1, "SAME");
  put num_date= num_date_minus_year=;
run;

Output:

num_date=03MAY2022 num_date_minus_year=03MAY2021

CodePudding user response:

You can get the current date using the DATE() function.

Do you want one YEAR or 365 days?

To use a date interval use the INTNX() function.

where expire_date >= intnx('year',date(),-1,'same') ;

To use a fixed number of days just subtract the number.

where expire_date >= date() - 365 ;
  • Related