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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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