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.
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.
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.
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.
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;
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.
Better to use FINDW() function .
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.
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?
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!
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.
Ready to level-up your skills? Choose your own adventure.