BookmarkSubscribeRSS Feed
kamlesh_suthar
Fluorite | Level 6

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

5 REPLIES 5
PaigeMiller
Diamond | Level 26

You can use the INTCK function, something like this

 

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

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
PGStats
Opal | Level 21

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
Rick_SAS
SAS Super FREQ

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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 1665 views
  • 0 likes
  • 6 in conversation