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

## Working with Strings

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
Garnet | Level 18

## Re: Working with Strings

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.

7 REPLIES 7
Pyrite | Level 9

## Re: Working with Strings

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.

Garnet | Level 18

## Re: Working with Strings

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.

Onyx | Level 15

## Re: Working with Strings

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;
Rhodochrosite | Level 12

## Re: Working with Strings

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.

Super User

## Re: Working with Strings

Better to use FINDW()  function .

Obsidian | Level 7

## Re: Working with Strings

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.

Tourmaline | Level 20

## Re: Working with Strings

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?

Discussion stats
• 7 replies
• 1334 views
• 12 likes
• 7 in conversation