BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
fengyuwuzu
Pyrite | Level 9

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?

1 ACCEPTED SOLUTION

Accepted Solutions
LinusH
Tourmaline | Level 20
You cannot use first. or last. variables within a where statement since where does the filtering in the input buffers. And that's prior to when the data hits the PDV (and that's where the first. variables are created).

If you wish to accomplish this in a single step look into SQL:
having order_date < min (order_date) +30
Data never sleeps

View solution in original post

6 REPLIES 6
LinusH
Tourmaline | Level 20
You cannot use first. or last. variables within a where statement since where does the filtering in the input buffers. And that's prior to when the data hits the PDV (and that's where the first. variables are created).

If you wish to accomplish this in a single step look into SQL:
having order_date < min (order_date) +30
Data never sleeps
fengyuwuzu
Pyrite | Level 9

that works! thank you for teaching me!

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

fengyuwuzu
Pyrite | Level 9
Thank you, RW9. This requires both ID and order_date need to be sorted, right? But there is only id in the by statement.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

fengyuwuzu
Pyrite | Level 9

Thank you, RW9.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 1589 views
  • 0 likes
  • 3 in conversation