Good afternoon.
I have two variables var1 and var2. Var1 may or may not be contained in var2. If var1 is contained in var2 I need to remove it and then concatenate it back to var2 with a hyphen between them. Alternatively, I could find var1 within var2 and place a hyphen between them. However, if var1 doesn't exist within var2 I'd still need to concatenate them with an intervening hyphen between them.
Example:
VAR1 VAR2 WANT
DSU DS-12B DSU-12B
MHUB B214B MHUB-214B
6 6-20A 6-20A
C3-J 3J10B C3-J10B
01 1-29B 1-29B
MHI MHI18 MHI-18
I would really appreciate any help someone out there might be able to give me.
@Jeff_DOC wrote:
Good afternoon.
I have two variables var1 and var2. Var1 may or may not be contained in var2. If var1 is contained in var2 I need to remove it and then concatenate it back to var2 with a hyphen between them. Alternatively, I could find var1 within var2 and place a hyphen between them. However, if var1 doesn't exist within var2 I'd still need to concatenate them with an intervening hyphen between them.
Example:
VAR1 VAR2 WANT
DSU DS-12B DSU-12B
MHUB B214B MHUB-214B
6 6-20A 6-20A
C3-J 3J10B C3-J10B
01 1-29B 1-29B
MHI MHI18 MHI-18
I would really appreciate any help someone out there might be able to give me.
I only see two instances of "var1 contained in var2". So you need to be a bit more specific on what "contained in" may actually mean.
You as a minimum need to consider the current defined lengths of your var1 and var2 variables. When you concatenate the Var1 value, plus a hyphen, you may exceed the current defined length of the Var2. So something needs to be done.
You should provide example data in the form of data step.
Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
You should use more precise phrasing to describe you problem because:
If this interpretation is what you have in mind, then this will do it (except for the note 5, so that the leading zero is kept) ;
data have ;
input var1 $ var2 $ ;
cards ;
DSU DS-12B
MHUB B214B
6 6-20A
C3-J 3J10B
01 1-29B
MHI MHI18
ABC XYZ
run ;
data want (drop = _:) ;
set have ;
newvar = var1 || var2 ; *set length;
_v1 = compress (var1, "-") ;
_v2 = compress (var2, "-") ;
_x = findc (_v2, var1, "tk") ;
newvar = catx ("-", _v1, substr (_v2, _x)) ;
run ;
As a result, the output NEWVAR values will appear as:
DSU-12B MHUB-214B 6-20A C3J-10B 01-29B MHI-18 ABC-XYZ
Kind regards
Paul D.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.