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

Hi,

I need help on below query -

I have below dataset. From this data I need to capture Client, Current_Product, First Instance of Previous_product (If there are any - Highlighted in blue colour) and PREVIOUS_PRODUCT_year_month.

Data is sorted by client, descending Year_month

Data -

CLIENT

Current_Product

YEAR_MONTH

1

ABC

202111

1

ABC

202110

1

ABC

202109

1

ABC

202108

1

ABC

202107

2

CDE

202111

2

CDE

202110

2

CDE

202109

2

CDF

202108

2

CDE

202107

 

Output I am looking for - 

 

CLIENT

Current_Product

PREVIOUS_PRODUCT

PREVIOUS_PRODUCT_year_month

1

ABC

 

 

2

CDE

CDF

202108

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Something like this, perhaps?

Data want;
  do until(last.client);
    set have(rename=(Current_Product=Product Year_month=Previous_Product_Year_month));
    by client Product notsorted;
    if first.client then 
      Current_Product=Product;
    else if first.product then do;
      Previous_Product=Product;
      output;
      end;
   end; 
   keep client Current_Product Previous_Product Previous_Product_Year_month;
run;

 

View solution in original post

1 REPLY 1
s_lassen
Meteorite | Level 14

Something like this, perhaps?

Data want;
  do until(last.client);
    set have(rename=(Current_Product=Product Year_month=Previous_Product_Year_month));
    by client Product notsorted;
    if first.client then 
      Current_Product=Product;
    else if first.product then do;
      Previous_Product=Product;
      output;
      end;
   end; 
   keep client Current_Product Previous_Product Previous_Product_Year_month;
run;

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 455 views
  • 1 like
  • 2 in conversation