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

Hi, 

 

I have these observations under the variable "var1":

Members United Swaps With Defendants
Members United Swaps With Other Parties
Southwest Swaps With Defendants
Southwest Swaps With Other Parties
WesCorp Swaps With Defendants
WesCorp Swaps With Other Parties
U.S. Central Swaps With Defendants
U.S. Central Swaps With Other Parties

 

Basically I would like to create a variable "var2" based on "var1"  that looks like this:

With Defendants
With Other Parties
With Defendants
With Other Parties
With Defendants
With Other Parties
With Defendants
With Other Parties

 

Can someone help me out?

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

 

data want;
 set have;
       length var2 $20;  /* adapt to max length expected */
       var2 = substr(var1, index(var1,'with'));
run;

Check weather you need to check case (upcase / lowcase) of 'with' letters. 

View solution in original post

7 REPLIES 7
cau83
Pyrite | Level 9
if index(var1,"With Defendants") > 0 then var2="With Defendants";
else var2="With Other Parties";

index() is a character function where the first argument is the string/column you want to search, and second argument is what you want to search for. If that second argument is found, the function returns a number equal to the starting position. Else, it returns 0. So the above looks to see if it contains defendants, and if not, assumes it contains parties. You could add other if else/else logic if necessary.

Shmuel
Garnet | Level 18

 

data want;
 set have;
       length var2 $20;  /* adapt to max length expected */
       var2 = substr(var1, index(var1,'with'));
run;

Check weather you need to check case (upcase / lowcase) of 'with' letters. 

Haikuo
Onyx | Level 15

This is to extract and thing after 'with' including 'with',

 

data test;
infile cards truncover ;
input var1 $100.;
new_var=prxchange('s/.+(with.+)/$1/io',-1,var1);
cards;
Members United Swaps With Defendants
Members United Swaps With Other Parties
Southwest Swaps With Defendants
Southwest Swaps With Other Parties
WesCorp Swaps With Defendants
WesCorp Swaps With Other Parties
U.S. Central Swaps With Defendants
U.S. Central Swaps With Other Parties
;
run;
KachiM
Rhodochrosite | Level 12

You may also use the @'character' column pointer.

data test;
infile cards truncover ;
input @'Swaps' new_var &$20.;
cards;
Members United Swaps With Defendants
Members United Swaps With Other Parties
Southwest Swaps With Defendants
Southwest Swaps With Other Parties
WesCorp Swaps With Defendants
WesCorp Swaps With Other Parties
U.S. Central Swaps With Defendants
U.S. Central Swaps With Other Parties
;
run;

Cheers.

Ksharp
Super User

Better to use FINDW()  function .

jrdykstr93
Obsidian | Level 7

I ended up using this code: 

 

new_var1= strip(substr(var_have,findw(upcase(var_have),'WITH')+lengthc('with')));

new_var2 = strip(substr(var_have,1,findw(upcase(var_have),'WITH')-1));

 

Because it turned i needed to make two variables, one before with and one after.  I am sure there is better ways to do this, but as of right now this way is working.  Thanks for everyone's contribution.

ChrisNZ
Tourmaline | Level 20

This does not even do what you described.

data t;
  VAR_HAVE = 'Members United Swaps With Other Parties';
  NEW_VAR1 = strip(substr(VAR_HAVE,findw(upcase(VAR_HAVE),'WITH')+lengthc('with')));
  NEW_VAR2 = strip(substr(VAR_HAVE,1,findw(upcase(VAR_HAVE),'WITH')-1));
run;
VAR_HAVE NEW_VAR1 NEW_VAR2
Members United Swaps With Other Parties Other Parties Members United Swaps

A variation on @Shmuel's reply is the simplest:

data want;
  set have;
  length var2 $20; 
  var2 = substr(var1, findw(var1,'with',' ','i'));
run;

What's wrong with that?

 

 

 

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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
  • 7 replies
  • 1722 views
  • 12 likes
  • 7 in conversation