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
You can use the INTCK function, something like this
data want;
set have;
if intck(launchdate,date)>6 then output;
run;
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.
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;
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;
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.
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.
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.