<?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 How to match two variables from two tables and generate a new variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707609#M217278</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;This is what I have,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE1;
    INPUT (NAME1) (:$8.);
    CARDS;
Chicken
Dinner
Winner
Pubg
;
run;

DATA LIST;
    INPUT (NAME2) (:$8.);
    CARDS;
en
bg
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is what I want&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WANT;
    INPUT (NAME1 DUMMY) (:$8.);
    CARDS;
Chicken 1
Dinner 0
Winner 0
Pubg 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The basic logic is&lt;/P&gt;&lt;P&gt;IF HAVE1.NAME1 contains LIST.NAME2 then WANT.DUMMY = 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I try to use SQL and LEFT JOIN but I am not sure how to create a new dummy variable here.&lt;/P&gt;&lt;P&gt;I think using the DATA step could also work out, using _null_ to define a third data or something.&lt;/P&gt;&lt;P&gt;But anyway, I wonder if you could please help me with this.&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
    <pubDate>Tue, 22 Dec 2020 05:58:32 GMT</pubDate>
    <dc:creator>yanshuai</dc:creator>
    <dc:date>2020-12-22T05:58:32Z</dc:date>
    <item>
      <title>How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707609#M217278</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;This is what I have,&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE1;
    INPUT (NAME1) (:$8.);
    CARDS;
Chicken
Dinner
Winner
Pubg
;
run;

DATA LIST;
    INPUT (NAME2) (:$8.);
    CARDS;
en
bg
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is what I want&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WANT;
    INPUT (NAME1 DUMMY) (:$8.);
    CARDS;
Chicken 1
Dinner 0
Winner 0
Pubg 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;The basic logic is&lt;/P&gt;&lt;P&gt;IF HAVE1.NAME1 contains LIST.NAME2 then WANT.DUMMY = 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I try to use SQL and LEFT JOIN but I am not sure how to create a new dummy variable here.&lt;/P&gt;&lt;P&gt;I think using the DATA step could also work out, using _null_ to define a third data or something.&lt;/P&gt;&lt;P&gt;But anyway, I wonder if you could please help me with this.&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 05:58:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707609#M217278</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2020-12-22T05:58:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707614#M217282</link>
      <description>&lt;P&gt;Next code is tested:&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
 set list end=eof;
     retain list; 
     length list $32000;
     list = catx(',',list,quote(strip(name2)));
     if eof then do;
        call symput('LIST',strip(list));
        call symput('qty',strip(put(_N_,best.)));
     end;
run;
%put &amp;amp;=list &amp;amp;=qty;

data want;
   array list{*} $ chk1-chk&amp;amp;qty (&amp;amp;list);
   set have1;
       dummy=0;
       do i=1 to dim(list);
	      if index(name1,strip(list(i))) 
		     then do; dummy=1; leave; end;
	   end;
	   keep name1 dummy;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 13:29:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707614#M217282</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-12-22T13:29:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707616#M217283</link>
      <description>&lt;P&gt;Try:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   array names[1000] $ 8 _TEMPORARY_;
   
   do _n_ = 1 by 1 until (listRead);
      set work.list end=listRead;
      names[_n_] = name2;
   end;
   
   lastElement = _n_;

   do _n_ = 1 by 1 until(done);
      set work.have1 end=done;
      
      dummy = 0;
      
      do i = 1 to lastElement until(dummy);
         dummy = find(name1, names[i], 'it') &amp;gt; 0;
      end;
      
      output;
   end;

   keep name1 dummy;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You should adjust the array statement: the dimension of the array could to small, and if the entries in LIST are longer than eight chars, you have to increase then length, too.&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 08:36:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707616#M217283</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2020-12-22T08:36:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707671#M217305</link>
      <description>&lt;P&gt;When PROC SQL is a better solution, IMO&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
   CREATE TABLE WORK.WANT AS 
   SELECT distinct HAVE1.NAME1, 
          /*LIST.NAME2, */
            MAX(find(HAVE1.NAME1,LIST.NAME2,'ti') &amp;gt; 0) AS DUMMY
      FROM WORK.HAVE1 HAVE1,WORK.LIST LIST
      GROUP BY HAVE1.NAME1;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 14:50:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707671#M217305</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2020-12-22T14:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707681#M217313</link>
      <description>&lt;P&gt;Using Hash objects.&amp;nbsp; This should, idealistically, be fast for large files.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (Keep=NAME1 DUMMY) ;
  if 0 then set Work.LIST;
  declare hash _list(dataset: 'Work.LIST');
  rc= _list.defineKey('name2')
    + _list.defineData('name2')
    + _list.defineDone( );
  declare hiter list('_list');

  do until (eof);
    set have1 end=eof;

    rc = list.first();       
    DUMMY=0;
    do while (rc = 0 and DUMMY=0);
       put "NOTE: " _all_;
       DUMMY = find(name1, name2, 'it') &amp;gt; 0;
       rc = list.next();
    end;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 15:09:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707681#M217313</guid>
      <dc:creator>PhilC</dc:creator>
      <dc:date>2020-12-22T15:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707779#M217358</link>
      <description>&lt;P&gt;&amp;nbsp;This is awesome. I love SQL!&lt;/P&gt;&lt;P&gt;I wonder if I could do this with some macros&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO dummy(list)
DATA want;
SET have1;
IF FIND(have1.name1,'&amp;amp;list', 'i') ge 1 THEN dummy =1;
RUN;
%MEND;

DATA _null_;
SET list;
CALL EXECUTE('%dummy('name2');');
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This should be fast. but every time I run this macro, the outcome dummy = empty value&lt;/P&gt;&lt;P&gt;I don't know how to make the macro recognize and replace the '&amp;amp;list' with name2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 22 Dec 2020 22:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707779#M217358</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2020-12-22T22:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707793#M217365</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I think I figure it out&lt;/P&gt;&lt;P&gt;I use&lt;/P&gt;&lt;P&gt;%unqote(%str(%')&amp;amp;name2%str(%') to include single quotation in the macro variable&lt;/P&gt;</description>
      <pubDate>Wed, 23 Dec 2020 00:40:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707793#M217365</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2020-12-23T00:40:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707929#M217448</link>
      <description>&lt;P&gt;Hi guys,&lt;/P&gt;&lt;P&gt;I figure it out&lt;/P&gt;&lt;P&gt;here is what I did&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro list(identifier);
data have1;
   set have1;
   if find(name1,%unquote(%str(%')&amp;amp;identifier%str(%')),'i') ge 1 THEN dummy =1;
run;
%mend;


data _null_;
set list;
call execute('%list('!!name2!!');');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I love macro and SQL!&lt;/P&gt;</description>
      <pubDate>Wed, 23 Dec 2020 17:07:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/707929#M217448</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2020-12-23T17:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/708010#M217490</link>
      <description>&lt;P&gt;Why are you jumping through hoops to add quotes?&amp;nbsp; Macro triggers (&amp;amp; and %) are not evaluated inside of strings that start with single quotes.&amp;nbsp; Why do you want to use single quotes instead of double quotes?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;find(name1,"&amp;amp;identifier",'i')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you need them then just pass the string in with the quotes already in it.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;... find(name1,&amp;amp;identifier,'i') ...
%list('xyz')&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you could use the QUOTE() function when generating the macro call in your data step.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set list;
  call execute(cats('%nrstr(%list)(',quote(trim(name2),"'"),')'));
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 Dec 2020 21:45:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/708010#M217490</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-12-23T21:45:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to match two variables from two tables and generate a new variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/708164#M217568</link>
      <description>&lt;P&gt;Thansk.&lt;/P&gt;&lt;P&gt;I am not so sure about the difference between double quotes and single quotes.&lt;/P&gt;&lt;P&gt;But your second line of codes is a very clever way of solving using single quotes in macros.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the reply&lt;/P&gt;</description>
      <pubDate>Fri, 25 Dec 2020 08:18:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-match-two-variables-from-two-tables-and-generate-a-new/m-p/708164#M217568</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2020-12-25T08:18:41Z</dc:date>
    </item>
  </channel>
</rss>

