<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: compute how many strings of one data set exist in one character variable from a other data set. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/831947#M328827</link>
    <description>&lt;P&gt;First, I'll create some test data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create the test data */
data codes;
	drop _:;
	do _i=1 to 5;
		code=cats('code',_i);
		output;
	end;
run;

data have;
	call streaminit(12345);
	length id 8 text $100;
	/* Drops all variables with _ as the first character of the name */
	drop _:;
	do id=1 to 5;
		do _i=1 to rand('integer',1,4);
			Text=cats(Text,'code',rand('integer',1,5),'other random text');
		end;
		output;
		call missing(text);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks like this:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;CODES&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l b header" scope="col"&gt;code&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;&lt;STRONG&gt;HAVE&lt;/STRONG&gt;&lt;BR /&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;text&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;code5other random textcode3other random textcode5other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random textcode4other random textcode2other random textcode3other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;code3other random textcode5other random textcode2other random textcode5other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="l data"&gt;code4other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now to do the work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Find out how may codes there are, write number to macro variable */
proc sql noprint;
select count(*) into :dimension
   from codes
;
quit;

data want;
	drop _: code;
	/* Create a temporary array to hold al the code values */
	array lookup[&amp;amp;dimension] $100 _temporary_;
	/* Load the array with codes from your codes table */
   if _n_ = 1 then do;
      do _i=1 to &amp;amp;dimension;
         set codes end=EndOfLookup;
         lookup[_i]=code;
         end;
      end;
	/* read the data you want to test */
	set have2;
	/* reset the count for each row of input data */
	call missing (count);
	/* Add up the matches for each of the codes */
	do _i=1 to dim(lookup);
	   count+count(text,lookup[_i],'it');
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the result:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;text&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;count&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;code5other random textcode3other random textcode5other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random textcode4other random textcode2other random textcode3other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;code3other random textcode5other random textcode2other random textcode5other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="l data"&gt;code4other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
    <pubDate>Tue, 06 Sep 2022 14:11:56 GMT</pubDate>
    <dc:creator>SASJedi</dc:creator>
    <dc:date>2022-09-06T14:11:56Z</dc:date>
    <item>
      <title>compute how many strings of one data set exist in one character variable from a other data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/831921#M328814</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;I need your help for the below problem.&lt;/P&gt;&lt;P&gt;I have 2 data sets.For example data1 and data2.The data1 data set has codes like :&lt;/P&gt;&lt;P&gt;data1:&lt;/P&gt;&lt;P&gt;code1&lt;/P&gt;&lt;P&gt;code2&lt;/P&gt;&lt;P&gt;code3&lt;/P&gt;&lt;P&gt;code4&lt;/P&gt;&lt;P&gt;code5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and the data 2 data set has combinations of the above codes like:&lt;/P&gt;&lt;P&gt;code2code5&lt;/P&gt;&lt;P&gt;code1code5code4&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;My question is exist some way to create one new&amp;nbsp;column in data2 data set which it contains how many codes from the data1 appear in each row of data2?&lt;/P&gt;&lt;P&gt;Thank you in advance,&lt;/P&gt;&lt;P&gt;Burney 1998.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Sep 2022 12:59:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/831921#M328814</guid>
      <dc:creator>Barney1998</dc:creator>
      <dc:date>2022-09-06T12:59:54Z</dc:date>
    </item>
    <item>
      <title>Re: compute how many strings of one data set exist in one character variable from a other data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/831947#M328827</link>
      <description>&lt;P&gt;First, I'll create some test data:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Create the test data */
data codes;
	drop _:;
	do _i=1 to 5;
		code=cats('code',_i);
		output;
	end;
run;

data have;
	call streaminit(12345);
	length id 8 text $100;
	/* Drops all variables with _ as the first character of the name */
	drop _:;
	do id=1 to 5;
		do _i=1 to rand('integer',1,4);
			Text=cats(Text,'code',rand('integer',1,5),'other random text');
		end;
		output;
		call missing(text);
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It looks like this:&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;CODES&lt;/STRONG&gt;&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l b header" scope="col"&gt;code&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;code5&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV class="branch"&gt;&lt;STRONG&gt;HAVE&lt;/STRONG&gt;&lt;BR /&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;text&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;code5other random textcode3other random textcode5other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random textcode4other random textcode2other random textcode3other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;code3other random textcode5other random textcode2other random textcode5other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="l data"&gt;code4other random text&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now to do the work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Find out how may codes there are, write number to macro variable */
proc sql noprint;
select count(*) into :dimension
   from codes
;
quit;

data want;
	drop _: code;
	/* Create a temporary array to hold al the code values */
	array lookup[&amp;amp;dimension] $100 _temporary_;
	/* Load the array with codes from your codes table */
   if _n_ = 1 then do;
      do _i=1 to &amp;amp;dimension;
         set codes end=EndOfLookup;
         lookup[_i]=code;
         end;
      end;
	/* read the data you want to test */
	set have2;
	/* reset the count for each row of input data */
	call missing (count);
	/* Add up the matches for each of the codes */
	do _i=1 to dim(lookup);
	   count+count(text,lookup[_i],'it');
   end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And the result:&lt;/P&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="left"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col"&gt;id&lt;/TH&gt;
&lt;TH class="l b header" scope="col"&gt;text&lt;/TH&gt;
&lt;TH class="r b header" scope="col"&gt;count&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;TD class="l data"&gt;code5other random textcode3other random textcode5other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;2&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random textcode4other random textcode2other random textcode3other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;TD class="l data"&gt;code3other random textcode5other random textcode2other random textcode5other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;4&lt;/TD&gt;
&lt;TD class="l data"&gt;code2other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="r data"&gt;5&lt;/TD&gt;
&lt;TD class="l data"&gt;code4other random text&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Tue, 06 Sep 2022 14:11:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/831947#M328827</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2022-09-06T14:11:56Z</dc:date>
    </item>
    <item>
      <title>Re: compute how many strings of one data set exist in one character variable from a other data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/832049#M328860</link>
      <description>thank you so much!&lt;BR /&gt;</description>
      <pubDate>Tue, 06 Sep 2022 20:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/832049#M328860</guid>
      <dc:creator>Barney1998</dc:creator>
      <dc:date>2022-09-06T20:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: compute how many strings of one data set exist in one character variable from a other data set.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/832051#M328861</link>
      <description>&lt;P&gt;We need names for the datasets and the variables to write code.&lt;/P&gt;
&lt;P&gt;So lets call the first dataset LIST and the variable CODE.&lt;/P&gt;
&lt;P&gt;And lets call the second dataset HAVE and the variable STRING.&lt;/P&gt;
&lt;P&gt;And lets call the output dataset WANT and the new variable COUNT.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So just join the two and sum up the number of codes that appear at least once.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
  select a.string
       , sum(0&amp;lt;index(a.string,b.code))&amp;nbsp;as&amp;nbsp;count
&amp;nbsp;&amp;nbsp;from&amp;nbsp;have&amp;nbsp;a
&amp;nbsp;&amp;nbsp;   ,&amp;nbsp;list&amp;nbsp;b
 &amp;nbsp;group&amp;nbsp;by&amp;nbsp;a.string
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Sep 2022 20:13:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/compute-how-many-strings-of-one-data-set-exist-in-one-character/m-p/832051#M328861</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-09-06T20:13:16Z</dc:date>
    </item>
  </channel>
</rss>

