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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 845 views
  • 0 likes
  • 6 in conversation