<?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: How to count one variable according to different dataset's two variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494000#M130095</link>
    <description>&lt;P&gt;Seems pretty simple.&amp;nbsp; Just join the list of pairs the country product list twice.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table want as 
    select a.country1,a.country2,sum(b.product = c.product) as n_overlap
    from have1 a 
    left join have2 b on a.country1=b.country
    left join have2 c on a.country2=c.country
    group by 1,2
    order by 1,2
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    COUNTRY1    COUNTRY2    n_overlap

  1      001         002           1
  2      001         101           1
  3      001         103           1
  4      002         201           1
  5      002         202           1
  6      002         203           1
  7      003         203           2
  8      003         301           1
  9      003         302           0
 10      003         303           1
&lt;/PRE&gt;</description>
    <pubDate>Mon, 10 Sep 2018 04:51:30 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2018-09-10T04:51:30Z</dc:date>
    <item>
      <title>How to count one variable according to different dataset's two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/493996#M130094</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It has been a while since my last visit.&lt;/P&gt;
&lt;P&gt;Miss you guys.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A new issue kinda stuck me. I have two tables.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1 ;
input
COUNTRY1 COUNTRY2 $5.;
datalines;
001&amp;nbsp; 101
001&amp;nbsp; 002
001&amp;nbsp; 103
002&amp;nbsp; 201
002&amp;nbsp; 202
002&amp;nbsp; 203
003&amp;nbsp; 203
003&amp;nbsp; 301
003&amp;nbsp; 302
003&amp;nbsp; 303
;
run;

data have2 ;
input
COUNTRY PRODUCT $5.;
datalines;
001&amp;nbsp; A1
001&amp;nbsp; A2
001&amp;nbsp; A3
002&amp;nbsp; A3
002&amp;nbsp; A4
002&amp;nbsp; A5
003&amp;nbsp; A5
003&amp;nbsp; A6
003&amp;nbsp; A7
003&amp;nbsp; A8
101&amp;nbsp; A1
103&amp;nbsp; A3
201&amp;nbsp; A3
202&amp;nbsp; A4
203&amp;nbsp; A5
203&amp;nbsp; A8
301&amp;nbsp; A6
302&amp;nbsp; A4
303&amp;nbsp; A6
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;HAVE1 has two variables - each data point represent a country code.&lt;/P&gt;
&lt;P&gt;HAVE2's COUNTRY variables contains all the HAVE1 data points. Besides, there is a corresponding PRODUCT variable for each COUNTRY.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to have is, based on the pairs in HAVE1, to compute the number of OVERLAP in PRODUCT between COUNTRY1 and COUNTRY2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input
COUNTRY1 COUNTRY2 OVERLAP $5.;
datalines;
001  101  1
001  002  1
001  103  1
002  201  1
002  202  1
002  203  1
003  203  2
003  301  1
003  302  0
003  303  1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I actually can achieve what I want. But it takes hundreds of codes in a very inefficient way.&lt;/P&gt;
&lt;P&gt;Thus I wonder if there is any convenient way. I am exploring RIGHT JOIN or LEFT JOINT. But I don't know if I am right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Sep 2018 04:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/493996#M130094</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-09-10T04:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to count one variable according to different dataset's two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494000#M130095</link>
      <description>&lt;P&gt;Seems pretty simple.&amp;nbsp; Just join the list of pairs the country product list twice.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table want as 
    select a.country1,a.country2,sum(b.product = c.product) as n_overlap
    from have1 a 
    left join have2 b on a.country1=b.country
    left join have2 c on a.country2=c.country
    group by 1,2
    order by 1,2
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;Obs    COUNTRY1    COUNTRY2    n_overlap

  1      001         002           1
  2      001         101           1
  3      001         103           1
  4      002         201           1
  5      002         202           1
  6      002         203           1
  7      003         203           2
  8      003         301           1
  9      003         302           0
 10      003         303           1
&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Sep 2018 04:51:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494000#M130095</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-10T04:51:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to count one variable according to different dataset's two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494223#M130207</link>
      <description>&lt;P&gt;Thank you. This is exactly what I need.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I actually know it will use JOIN or LEFT JOIN or RIGHT JOIN. But I don't quite understand how those JOIN work. Thus it confuses me sometimes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot.&lt;/P&gt;</description>
      <pubDate>Mon, 10 Sep 2018 17:40:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494223#M130207</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-09-10T17:40:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to count one variable according to different dataset's two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494282#M130220</link>
      <description>&lt;P&gt;I wonder what if I have 20 such tables. How can I merge them in one table according to the date.&lt;/P&gt;&lt;P&gt;Instead of HAVE1, HAVE2. Now I have&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1 ;
input
COUNTRY1 YEAR COUNTRY2 $5.;
datalines;
001  2000  101
001  2000  002
001  2001  103
002  2000  201
002  2000  202
002  2001  203
003  2001  203
003  2000  301
003  2000  302
003  2001  303
;
run;

data have2000 ;
input
COUNTRY PRODUCT $5.;
datalines;
001  A1
001  A2
001  A3
002  A3
002  A4
002  A5
003  A5
003  A6
003  A7
003  A8
101  A1
103  A3
201  A3
202  A4
203  A5
203  A8
301  A6
302  A4
303  A6
;
run;

data have2001 ;
input
COUNTRY PRODUCT $5.;
datalines;
001  A3
001  A2
001  A1
002  A5
002  A4
002  A3
003  A4
003  A8
003  A7
003  A9
101  A1
103  A3
201  A3
202  A4
203  A6
203  A7
301  A5
302  A3
303  A5
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I would like to have is to compute the OVERLAP only for the YEAR in HAVE1.&lt;/P&gt;&lt;P&gt;Shall I use WHERE condition?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Sep 2018 19:34:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494282#M130220</guid>
      <dc:creator>yanshuai</dc:creator>
      <dc:date>2018-09-10T19:34:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to count one variable according to different dataset's two variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494316#M130230</link>
      <description>&lt;P&gt;Would be easier if you combined your by year tables into a single table with a YEAR variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have2 ;
   length dsname $50 ;
   set have2001-have2003 indsname=dsname ;
   length YEAR $4 ;
   year = substr(dsname,length(dsname)-3);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Sep 2018 20:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-count-one-variable-according-to-different-dataset-s-two/m-p/494316#M130230</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-09-10T20:38:39Z</dc:date>
    </item>
  </channel>
</rss>

