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!
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;
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;
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.
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.