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;

 

sas-innovate-2024.png

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.

 

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
  • 1 reply
  • 161 views
  • 1 like
  • 2 in conversation