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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 739 views
  • 0 likes
  • 5 in conversation