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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.