BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ismahero2
Obsidian | Level 7

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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);  
--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
ismahero2
Obsidian | Level 7
I agree.. I cannot use real data. but I discovered the problem I am having is related to the length of the data. The codes in the real data have different lengths... For example: KK109Y40,Y744P0,K444U7,H402O2,Y4449HH898O9,YY4930,TT785U983I9

So in this example, the lengths are 11, 6, 12, 6, 6, 7. So, if I leave the array like this "Array RejC{4} $ (&RejCode);" it will truncate the values to the length of the first one It reads. However, if I use the length of the longest values like this: Array RejC{4} $12 (&RejCode); it doesn't like it. But if I use $11 instead of the longest, then it works. Have you ever seen something like this? Any ideas?
PaigeMiller
Diamond | Level 26

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);  
--
Paige Miller

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