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

Hi everyone,

 

I have this table:

Field1 Field2Field3Field4Field5Field6
AAAAAADIFVMXIRUBBB
VCXKEMCCCBBBAAAAAA
EIEOFPMCNUVNDDKJDN

 

I also have a lookup table:

Field
AAA
BBB
CCC

 

I want to count the fields that contain the fields in the lookup table (it needs to be contains as there may not be exact matches):

 

Field1 Field2Field3Field4Field5Field6COUNT
AAAAAADIFVMXIRUBBB3
VCXKEMCCCBBBAAAAAA4
EIEOFPMCNUVNDDKJDN0

 

Hope someone can help. There are more than 6 fields so a bunch of 'OR's will not work. I prefer the solution to be in SAS language, not SQL. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Here is one way.

 

data have;
input (Field1-Field6)(:$10.);
datalines;
AAA AAA DIF VMX IRU BBB
VCX KEM CCC BBB AAA AAA
EIE OFP MCN UVN DDK JDN
;

data lookup;
input Field $;
datalines;
AAA
BBB
CCC
;

data want(keep=Field1-Field6 Count);
   set have;
   array a{*} $ Field1-Field6;
   count=0;
   do i=1 to n;
      set lookup point=i nobs=n;
      do j=1 to dim(a);
         if (a[j]=Field) then count+1;
      end;
   end;
run;

View solution in original post

6 REPLIES 6
ballardw
Super User

How many values are in the look up table?

How volatile are the values or how often do the values in the look up table change?

PeterClemmensen
Tourmaline | Level 20

Here is one way.

 

data have;
input (Field1-Field6)(:$10.);
datalines;
AAA AAA DIF VMX IRU BBB
VCX KEM CCC BBB AAA AAA
EIE OFP MCN UVN DDK JDN
;

data lookup;
input Field $;
datalines;
AAA
BBB
CCC
;

data want(keep=Field1-Field6 Count);
   set have;
   array a{*} $ Field1-Field6;
   count=0;
   do i=1 to n;
      set lookup point=i nobs=n;
      do j=1 to dim(a);
         if (a[j]=Field) then count+1;
      end;
   end;
run;
kz134
Obsidian | Level 7
Thanks. This worked. If we remove (keep=Field1-Field6 Count), we get a column called Field in the want table. And the value is CCC. For learning purposes, what does this mean?
PeterClemmensen
Tourmaline | Level 20

The Field variable is from the lookup data set. Try running this code. Might clear up the logic 

 

data want;
   set have;
   array a{*} $ Field1-Field6;
   count=0;
   do i=1 to n;
      set lookup point=i nobs=n;
      do j=1 to dim(a);
         if (a[j]=Field) then count+1;
      end;
      output;
   end;
run;
andreas_lds
Jade | Level 19
Variable field contains the last value in the lookup dataset.
Ksharp
Super User
data have;
input (Field1-Field6)(:$10.);
datalines;
AAA AAA DIF VMX IRU BBB
VCX KEM CCC BBB AAA AAA
EIE OFP MCN UVN DDK JDN
;

data lookup;
input Field $;
datalines;
AAA
BBB
CCC
;

data want;
 if _n_=1 then do;
  if 0 then set lookup;
  declare hash h(dataset:'lookup');
  h.definekey('field');
  h.definedone();
 end;
set have;
array x{*} $ Field1-Field6 ;
count=0;
do i=1 to dim(x);
  field=x{i};
  if h.check()=0 then count+1;
end;
drop i;
run;

SAS Innovate 2025: Register Today!

 

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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
  • 6 replies
  • 1079 views
  • 0 likes
  • 5 in conversation