BookmarkSubscribeRSS Feed
ScotchCat
Obsidian | Level 7

Be gentle, I'm new to SAS.  I need to find the File_Date each time the Product changed.  Thanks for assisting! 

Data have: 

File_date Product
12/1/2023 car
12/2/2023 car
12/3/2023 car
12/4/2023 car
12/5/2023 car
12/6/2023 truck
12/7/2023 truck
12/8/2023 truck
12/9/2023 truck
12/10/2023 truck
12/11/2023 bike
12/12/2023 bike
12/13/2023 bike

 

Data I'd like to have: 

File_date Product
12/1/2023 car
12/6/2023 truck
12/11/2023 bike
2 REPLIES 2
PaigeMiller
Diamond | Level 26
data want;
    set have;
    prev_product=lag(product);
    if product^=prev_product then output;
    drop prev_product;
run;
--
Paige Miller
ballardw
Super User

Assuming your data is grouped by the product and has the file date in sequence:

 

data want;
   set have;
   by product notsorted;
   if first.product;
run;

When you use BY in a data step SAS provides automatic variables First and Last to indicate if the observation is the first or last of the group. The Notsorted allows use of data that is grouped but not actually sorted by the BY variables.

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 412 views
  • 2 likes
  • 3 in conversation