I have 3 variables, ID, order_date, and order_amount for a year, and I want to get the first 30days data starting from the first order date for each ID.
proc sort data=year_data;
by ID order_date;
run;
data first_month;
set year_data;
by ID order_date;
where day(order_date) - day(first.order_date) <=30;
run;
this does not work. There is a syntax error at day(first.order_date). What is wrong there?
that works! thank you for teaching me!
Well, you could do something like:
data first_month;
set year_data;
by id;
retain first_date;
if first.if then first_date=order_date;
if first_date <= order_date <= intnx('month',first_date,1,'same') then output;
run;
Note I am assuming that date is a date variable not a datetime. This will output only rows between first_date and first_date+1 month.t
Yes, it reuiqres the data to be sorted, as per the proc sort in your first post. The date does not need to be in the by line however, what I am doing is retaining the first observation for each id across the id, then using that to compare to the current observations date.
Thank you, RW9.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.