BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
frupaul
Quartz | Level 8

Hi Everyone,

 

I have the following data set:

 

Screen Shot 2018-07-18 at 12.37.10.png

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:

Screen Shot 2018-07-18 at 12.40.53.png

 

Any ideas on how I can do this please?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

4 REPLIES 4
andreas_lds
Jade | Level 19

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.

 

Tom
Super User Tom
Super User

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;
frupaul
Quartz | Level 8

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

Ksharp
Super User
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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 2383 views
  • 0 likes
  • 4 in conversation