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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.