DATA Step, Macro, Functions and more

Determining date difference

Reply
Occasional Contributor
Posts: 8

Determining date difference

[ Edited ]

Hi,

 

want to code the following

assume i have a data set which contains time series date, product id and launch date if the difference between the launch date and date is more than 6 month, we should consider the history of that product and take all records for analysis, else ignore the product.

Product id date launch date
1 201710 201710
1 201711 201710
1 201712 201710
1 201801 201710
1 201802 201710
2 201710 201706
2 201711 201706
2 201712 201706
2 201801 201706
2 201802 201706

 

In the above since product 1 was launched in 201710, and it is not 6 month old, we should ignore the history of this product, while product 2 is already 6 month old now hence we should consider this product.

 

 

Thanks in advance

Respected Advisor
Posts: 2,810

Re: Determining date difference

[ Edited ]
Posted in reply to kamlesh_suthar

You can use the INTCK function, something like this

 

data want;
    set have;
    if intck(launchdate,date)>6 then output;
run;
--
Paige Miller
Super User
Posts: 9,880

Re: Determining date difference

Posted in reply to kamlesh_suthar

You posted this question in the "Base SAS Programming" community. Do you think that "base sas" (sic) is a descriptive subject (what you were asked for when hitting the submit button)?

 

Please be a little more creative next time around.

 

I will edit the subject of your initial post and the answers.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,402

Re: Determining date difference

[ Edited ]
Posted in reply to kamlesh_suthar

Not tested, post test data in the form of a datastep!

proc sql;
  create table want as
  select *
  from   have
  where product_id in (select distinct product_id from have where intck('year',launch_date,date) <=6);
quit;
Esteemed Advisor
Posts: 5,479

Re: Determining date difference

Posted in reply to kamlesh_suthar

Convert the dates to SAS dates and use function intck to calculate the number of months:

 

data have;
input Product _date _ldate;
date = mdy(mod(_date,100),1,floor(_date/100));
ldate = mdy(mod(_ldate,100),1,floor(_ldate/100));
drop _: ;
format date ldate yymmdd10.;
datalines; 
1 201710 201710 
1 201711 201710 
1 201712 201710 
1 201801 201710 
1 201802 201710 
2 201710 201706 
2 201711 201706 
2 201712 201706 
2 201801 201706 
2 201802 201706 
;

proc sql;
create table want as
select
    product,
    date,
    ldate
from
    have
group by product
having intck("month",min(ldate), max(date), "continuous") > 6;
select * from want;
quit;
PG
SAS Super FREQ
Posts: 4,171

Re: Determining date difference

To build on PGStats' response, the article 

"INTCK and INTNX: Two essential functions for computing intervals between dates in SAS"

contains more information, discussion, and an example of using the INTCK function and the 'CONTINUOUS' option.

Ask a Question
Discussion stats
  • 5 replies
  • 122 views
  • 0 likes
  • 6 in conversation