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.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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