Hello,
I want to determine whether CHAR2 variable is included in CHAR1 variable.
The data and the desired results are as follows:
Data:
NAME | YEAR | CHAR1 | CHAR2 |
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 |
Desired result:
NAME | YEAR | INCLUDE |
JOY | 2009 | 1 |
JOY | 2009 | 1 |
JOY | 2009 | 0 |
JOY | 2009 | 1 |
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.
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).
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).
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;
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.
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!
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.