BookmarkSubscribeRSS Feed
hwangnyc
Quartz | Level 8

Hi everyone,

 

I have string data in two columns that are separated by commas. I'd like to scan the first column's characters and see if it appears in the second column.

 

Here is some sample data:

 

HaveAHaveB
Homeless, Health Care, AidedHealth Care, Verified, Aided
A,B,CA,C
X,Y,ZJ,K,F

 

This is what I want:

 

HaveAHaveBWant
Homeless, Health Care, AidedHealth Care, Verified, AidedHealth Care, Aided
A,B,CA,CA,C
X,Y,ZJ,K,F.

 

Many thanks in advance!

1 REPLY 1
ballardw
Super User

Untested without data but has a chance of to get started.

 

data want;
   set have;
   length wantstr $100; /* or long enough to hold expected longest value*/
   do i= 1 to countw(HaveA,',');
      if index(haveb, scan(havea,i,','))>0 then wantstr=catx(',',wantstr,scan(havea,i,','));
   end;
   drop i;
run;

Countw  and Scan functions are told to only consider comma as delimiter for finding phrases,

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
  • 1 reply
  • 1020 views
  • 0 likes
  • 2 in conversation