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

 

data have;
input ID   rolename$;
cards; 
1      vp/ceo
1      division vp/ceo
1      global ceo/ cfo/ coo
2      vice ceo/cfo
2      vice ceo/cfo
2      division ceo/coo/cfo 
;

I want only strings starting with division and global to change to:

 

data have;
input ID   rolename$;
cards; 
1      vp/ceo
1      division vp/division ceo
1      global ceo/global cfo/global coo
2      vice ceo/division cfo
2      vice ceo/cfo
2      division ceo/division coo/division cfo 
;

Anyway to do? Eventually I want to detect that as long as the rolename contains "division ceo", the indicator will indicate 0.

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input ID   rolename $50.;
cards; 
1      vp/ceo
1      division vp/ceo
1      global ceo/cfo/coo
2      vice ceo/division cfo
2      vice ceo/cfo
2      division ceo/coo/cfo 
;
data want;
 set have;
 length want $ 200;
 want=rolename;
 if rolename =: 'division' then want=prxchange('s/\//\/division /o',-1,rolename);
 if rolename =: 'global'   then want=prxchange('s/\//\/global /o',-1,rolename);
run;

View solution in original post

6 REPLIES 6
Jarvin99
Obsidian | Level 7

Just an add on, if possible I want this split-and-rejoin thing to be a macro, as I actually have more starting words (not just "division" and "global", but also words like "regional" and "international") which I want to conduct this.

ballardw
Super User

@Jarvin99 wrote:

Just an add on, if possible I want this split-and-rejoin thing to be a macro, as I actually have more starting words (not just "division" and "global", but also words like "regional" and "international") which I want to conduct this.


I have a strong suspicion that adding in macro language will just make this more complicated. The bit you need to provide are the rules about when a specific value is to be used.

ballardw
Super User

First thing is to ensure that your posted data step creates the values you expect. Yours does not because the $ creates variables of length 8 and use of list input means that the first space encounters ends the value of "rolename".

 

Second, you will need to either specify a length for that variable that will allow inserting extra characters.

Here is an example data set that makes rolename long enough to insert some characters but you may have to provide much more exhaustive examples to know just how long it should be.

 

data have;
length id 8 rolename $ 100;
input ID   rolename 8-28;
cards; 
1      vp/ceo
1      division vp/ceo
1      global ceo/ cfo/ coo
2      vice ceo/cfo
2      vice ceo/cfo
2      division ceo/coo/cfo 
;

 

 

 

Now to heart of the problem.

What in the data tells us that the first occurrence of

vice ceo/cfo

is supposed to become

vice ceo/division cfo

but the second occurrence does not change???

 

Duplicating a first word separated by a space, which appeared to be a rule at first glance can be done relatively easily:

data want;
   set have;
   if not missing(scan(rolename,1,' ')) then 
        rolename= tranwrd(rolename,'/',cat('/',scan(rolename,1,' '),' '));
run;

but there appear to be a multitude of other rules involved as you insert a different word for some, nothing for others.

 

 

Jarvin99
Obsidian | Level 7

Original:

data have;
input ID   rolename$50;
cards; 
1      vp/ceo
1      division vp/ceo
1      global ceo/cfo/coo
2      vice ceo/division cfo
2      vice ceo/cfo
2      division ceo/coo/cfo 
;

Hope to achieve:

data have;
input ID   rolename$50;
cards; 
1      vp/ceo
1      division vp/division ceo
1      global ceo/global cfo/global coo
2      vice ceo/division cfo
2      vice ceo/cfo
2      division ceo/division coo/division cfo 
;

In this case, no macro is fine, but I need to keep the starting word flexible as I have other starting words (e.g., international ceo/cfo --> international ceo/international cfo) to apply.

 

 Sorry for the data input issue as I am not very familiar with this. Thank you so much for your help. 

Jarvin99
Obsidian | Level 7

Not all starting words I want to split and rejoin. In this case, only "division" and "global" I want to do this, but not for "vice". Thus, the issue is a bit troublesome.

Ksharp
Super User
data have;
input ID   rolename $50.;
cards; 
1      vp/ceo
1      division vp/ceo
1      global ceo/cfo/coo
2      vice ceo/division cfo
2      vice ceo/cfo
2      division ceo/coo/cfo 
;
data want;
 set have;
 length want $ 200;
 want=rolename;
 if rolename =: 'division' then want=prxchange('s/\//\/division /o',-1,rolename);
 if rolename =: 'global'   then want=prxchange('s/\//\/global /o',-1,rolename);
run;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 6 replies
  • 552 views
  • 0 likes
  • 3 in conversation