BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cool1993
Fluorite | Level 6

Hello,

 

I want to determine whether CHAR2 variable is included in CHAR1 variable.

 

The data and the desired results are as follows:

 

Data:

NAMEYEARCHAR1CHAR2
JOY2009A, B, C, DA, B, D
JOY2009A, B, DA, B, D
JOY2010A, DA, B, D
JOY2011A, B, C, DB

 

 

Desired result:

NAMEYEARINCLUDE
JOY20091
JOY20091
JOY20090
JOY20091

 

I tried the following solution, but it doesn't apply because the order of input is different.

 

INCLUDE = (indexw(CHAR1,CHAR2,', ')>0);


If you know the solution, please help me.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

So it looks like you don't want to test if the value of CHAR2 is in CHAR1 (which is sort of what that function did).

Instead it looks like you want to test if every item listed in in CHAR2 is also listed in CHAR1.  

To do that you will need a loop.

data want ;
  set have ;
  not_found=0;
  do i=1 to countw(char2,', ') until (not_found);
    not_found=not indexw(scan(char2,i,', '),char1);
  end;
run;

So for each record NOT_FOUND will be TRUE (have a value of 1) if there exists a word in CHAR2 that is NOT in CHAR1.  If ALL of words in CHAR2 are also in CHAR1 (or CHAR2 is empty) then NOT_FOUND will be FALSE (have a value of 0).

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

So it looks like you don't want to test if the value of CHAR2 is in CHAR1 (which is sort of what that function did).

Instead it looks like you want to test if every item listed in in CHAR2 is also listed in CHAR1.  

To do that you will need a loop.

data want ;
  set have ;
  not_found=0;
  do i=1 to countw(char2,', ') until (not_found);
    not_found=not indexw(scan(char2,i,', '),char1);
  end;
run;

So for each record NOT_FOUND will be TRUE (have a value of 1) if there exists a word in CHAR2 that is NOT in CHAR1.  If ALL of words in CHAR2 are also in CHAR1 (or CHAR2 is empty) then NOT_FOUND will be FALSE (have a value of 0).

novinosrin
Tourmaline | Level 20

Hi @cool1993  Will this work?

 


data have;
infile cards expandtabs truncover;
input NAME $	YEAR	CHAR1 & $10.	CHAR2  & $10.;
cards;
JOY	2009	A, B, C, D	  A, B, D
JOY	2009	A, B, D	A, B,  D
JOY	2010	A, D	A, B, D
JOY	2011	A, B, C, D	  B
;


data want;
set have;
include=countw(char2,', ')=countc(compress(char1,', '),compress(char2,','));
run;
Shmuel
Garnet | Level 18

Both CHAR1 and CHAR2 variables are strings that include lists.

Are the lists of characters (1 byte each) or list of strings ?

 

You can use function COUNTW to check how many members are in each of those variables,

select a member of the list to check using SCAN function,

Use DO loops to check each member of CHAR2, does it exist in CHAR1.

You can check existence using either FINDW function or INDEX function or compare two members selected by SCAN. 

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 3 replies
  • 987 views
  • 1 like
  • 4 in conversation