Hi Everyone,
I have the following data set:
The above data set can be created with the code:
data Cust;
input Cust_name $ Manager1 $ Manager1 $ Manager3 $ Manager4 $ Manager5 $;
cards;
Jason Paul . . James Bond
Maxmil Lucien Peter . . Pan
Pogba . . Kit ARYNA Inieata
;
My objective is to create two variables:
- Highest_Ranked_Manager: the highest ranked manager of each customer
-Next_ranked_manager: The next highest ranked manager.
I believe this is self explanatory but the expected outcome table is:
Any ideas on how I can do this please?
Thanks
Good idea. You can simplify and make it more robust.
data want;
set work.CUST;
length Highest_ranked_manager Next_Ranked_Manager $50 ;
Highest_ranked_manager = scan(catx('|', of Manager:), -1,'|');
Next_Ranked_Manager = scan(catx('|', of Manager:), -2,'|');
run;
If you don't know what length to define the new variables you could let SAS figure it out be replacing the LENGTH statement with two assignment statements. That will make the length match the length of the first variable.
Highest_ranked_manager = manager1;
Next_Ranked_Manager = manager1;
If all manager-names have just one word, you could use:
data want;
set work.CUST;
length
ManagerList $ 50
Highest_ranked_manager Next_Ranked_Manager $ 8
;
ManagerList = catx(' ', of Manager:);
Highest_ranked_manager = scan(ManagerList, countw(ManagerList));
Next_Ranked_Manager = scan(ManagerList, countw(ManagerList)-1);
keep Cust_name Highest_ranked_manager Next_Ranked_Manager;
run;
You need to adjust length of variables depending on the real cust dataset.
EDIT: If the names are more complex, than please add those cases to the example dataset and we shall see, if the code can be updated or arrays and loops are necessary.
Good idea. You can simplify and make it more robust.
data want;
set work.CUST;
length Highest_ranked_manager Next_Ranked_Manager $50 ;
Highest_ranked_manager = scan(catx('|', of Manager:), -1,'|');
Next_Ranked_Manager = scan(catx('|', of Manager:), -2,'|');
run;
If you don't know what length to define the new variables you could let SAS figure it out be replacing the LENGTH statement with two assignment statements. That will make the length match the length of the first variable.
Highest_ranked_manager = manager1;
Next_Ranked_Manager = manager1;
HI Andreas, Thanks for the proposed solution. It worked but i got a warning. Im guessing it was because of the use of the countw function. However, when i used the scan(ManagerList, -1) and scan(ManagerList, -2) it worked without a warning
data Cust;
input Cust_name $ Manager1 $ Manager1 $ Manager3 $ Manager4 $ Manager5 $;
cards;
Jason Paul . . James Bond
Maxmil Lucien Peter . . Pan
Pogba . . Kit ARYNA Inieata
;
data want;
set cust;
array x{*} $ manager:;
do i=dim(x) to 1 by -1;
if not missing(x{i}) then do;highest=x{i};call missing(x{i});leave;end;
end;
do i=dim(x) to 1 by -1;
if not missing(x{i}) then do;next_highest=x{i};leave;end;
end;
drop i manager:;
run;
proc print noobs;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.