BookmarkSubscribeRSS Feed
Jeff_DOC
Pyrite | Level 9

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.

2 REPLIES 2
ballardw
Super User

@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.

hashman
Ammonite | Level 13

@Jeff_DOC:

You should use more precise phrasing to describe you problem because:

  1. "VAR1 contained in VAR2" means that VAR1 stripped of the leading and trailing blanks is part of VAR2 as a whole.
  2. This is incompatible with your suggested output WANT since, as @ballardw has pointed out, there're only 2 cases in your data sample where strip(VAR1) is part of VAR2. 
  3. Judging from WANT, it rather appears that your "contained" means "any character of VAR1 is part of VAR2".
  4. Furthermore, it appears that if such characters are found, only the first instance of each should be removed from VAR2 starting from the left before the concatenation is done. 
  5. This interpretation is consistent with all of your data sample except for the penultimate record #5, since according to this logic, WANT in this record should be 01-29B instead of 1-29B. If you don't want to include leading zeroes in WANT, you should say so.

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.

  

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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