Hi,
I have a list of values I need to find if any of them is present in a variable.
Example: I have a dataset or macro variable with a list of values like this:
K109Y4
K509J3
H402O2
Y5555P3
And I have a dataset that contains a variable named codes. I want to know if any of those values are present in the data.
OBS Codes
1 K109Y4,Y744P0,K444U7,H402O2
2 K330O9,H402O2
3 J777R5,R555N2,P909M4
In my results datasets, I would like to see OBS 1 and 2 because at least one of the values in my list matches the data in the string.
I am thinking of using Index(Codes) but what I am struggling is creating a code that will cycle through all the values, and if it one is positive in the index() then output. And if one of them is positive stop the loop and continue with the next record after it outputs the data.
I was trying to use an array and the leave statement after the output but is not getting me what I need.
Any ideas?
%Let RejCode="K109Y4" "K509J3" "H402O2" "Y5555P3"; Data want; Set source: ; Array RejC{4} $ (&RejCode); Do i=1 to dim(RejC); if index(codes,RejC[i])>0 then do;
Output;
leave;
end;
end;
end;
Run;
You could use this to force all variables in the array to have length 12. Naturally, you would need to find the longest element and make sure you have use that length and not necessarily 12.
array rejc{4} $12 (&rejcode);
works for me. If the same code doesn't work for you, then we are not using the same data in data set SOURCE. Or are you getting errors in the log? (which you didn't say you were).
%Let RejCode="K109Y4" "K509J3" "H402O2" "Y5555P3";
data source;
infile cards truncover;
input obs codes $60.;
cards;
1 K109Y4,Y744P0,K444U7,H402O2
2 K330O9,H402O2
3 J777R5,R555N2,P909M4
;
Data want;
set source: ;
Array RejC{4} $ (&RejCode);
do i=1 to dim(RejC);
if index(codes,RejC[i])>0 then do;
Output;
leave;
end;
end;
Run;
You could use this to force all variables in the array to have length 12. Naturally, you would need to find the longest element and make sure you have use that length and not necessarily 12.
array rejc{4} $12 (&rejcode);
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.