<?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 do I count elements in one data column with criteria from another column of data? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615672#M18621</link>
    <description>&lt;P&gt;brilliant. i will give it a go and get back with the result.&lt;/P&gt;</description>
    <pubDate>Tue, 07 Jan 2020 14:35:00 GMT</pubDate>
    <dc:creator>ejarquejm</dc:creator>
    <dc:date>2020-01-07T14:35:00Z</dc:date>
    <item>
      <title>How do I count elements in one data column with criteria from another column of data?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615607#M18618</link>
      <description>&lt;P&gt;/* I have a dataset with 4 data columns, COL 1: firm identifier 1&amp;nbsp; COL 2: worker identifier 1&amp;nbsp;(period 1), COL3: firms identifer 2, COL 4 worker identifier2 (period 2).&lt;/P&gt;&lt;P&gt;It could look like this for 5 firms and 100 workers:&lt;/P&gt;&lt;P&gt;*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; datain;&lt;/P&gt;&lt;P&gt;input Firmid1 $&amp;nbsp; w1 $ Firmid2 $ w2 $;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;030 32 010 11&lt;/P&gt;&lt;P&gt;030 45 010 79&lt;/P&gt;&lt;P&gt;030 64 020 05&lt;/P&gt;&lt;P&gt;040 07 030 21&lt;/P&gt;&lt;P&gt;040 21 030 32&lt;/P&gt;&lt;P&gt;040 72 030 45&lt;/P&gt;&lt;P&gt;050 12 040 07&lt;/P&gt;&lt;P&gt;050 13 040 72&lt;/P&gt;&lt;P&gt;050 87 040 81&lt;/P&gt;&lt;P&gt;050 99 050 13&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 050 98&lt;/P&gt;&lt;P&gt;;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Note that worker 21 switches&amp;nbsp; from firm 4 to firm 3. Worker 64 i only employed in period 1 and worker 79 is only employed in period 2, and firms 1 and 2 do not exist in period 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to do the following:&amp;nbsp;pick the firm indicator in period 1 (COL1) and count how many workers are attached to that firm in period 1, then go check for the existence of the same firm identifier in COL 3 and look in column 4 how many workers are in this particular firm in both periods.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output of this routine would be data looking like this: firm i.d.,&amp;nbsp; number of workers in period 1, number of workers in period 2, number of workers that stayed in the firm from period 1 to period 2. For the example above (where we are counting also from the perspective of column 3 if we can't find the firm in column 1) we get&amp;nbsp;&lt;/P&gt;&lt;P&gt;*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;data&lt;/STRONG&gt; dataout;&lt;/P&gt;&lt;P&gt;input Firmid $ n1 $ n2 $ nstay $;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;010 0 2 0&lt;/P&gt;&lt;P&gt;020 0 2 0&lt;/P&gt;&lt;P&gt;030 3 3 2&lt;/P&gt;&lt;P&gt;040 3 3 2&lt;/P&gt;&lt;P&gt;050 4 2 1&lt;/P&gt;&lt;P&gt;;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*&lt;/P&gt;&lt;P&gt;It is easy to do a frequency table for the periods separately. It is the counting of workers that remain across periods that is causing me trouble.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;J&lt;/P&gt;&lt;P&gt;PS (SAS version 9.4)&lt;/P&gt;&lt;P&gt;*/&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 09:49:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615607#M18618</guid>
      <dc:creator>ejarquejm</dc:creator>
      <dc:date>2020-01-07T09:49:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count elements in one data column with criteria from another column of data?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615651#M18620</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305717"&gt;@ejarquejm&lt;/a&gt;&amp;nbsp;and welcome to the SAS Support Communities!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can derive n1, n2 and (in particular) nstay with PROC SQL queries and join the three resulting tables:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table dataout as
select firmid1 as firmid, max(n1,0) as n1, max(n2,0) as n2, max(nstay,0) as nstay
from ((select firmid1, count(distinct w1) as n1
       from datain
       where firmid1
       group by firmid1)
         natural full join
      (select firmid2 as firmid1, count(distinct w2) as n2
       from datain
       where firmid2
       group by firmid2))
         natural left join
     (select firmid1, count(w1) as nstay from
       (select *
        from (select firmid1, w1 from datain)
               intersect
             (select firmid2, w2 from datain))
      group by firmid1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I found it more natural to create n1, n2 and nstay as &lt;EM&gt;numeric&lt;/EM&gt; variables, but feel free to convert them to character in the SELECT statement (using the PUT function), if needed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that n2=1 (not 2) for firmid '020'.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 13:14:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615651#M18620</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2020-01-07T13:14:22Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count elements in one data column with criteria from another column of data?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615672#M18621</link>
      <description>&lt;P&gt;brilliant. i will give it a go and get back with the result.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jan 2020 14:35:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615672#M18621</guid>
      <dc:creator>ejarquejm</dc:creator>
      <dc:date>2020-01-07T14:35:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do I count elements in one data column with criteria from another column of data?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615878#M18628</link>
      <description>&lt;P&gt;It of course works. Thank you.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Jan 2020 09:56:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-count-elements-in-one-data-column-with-criteria-from/m-p/615878#M18628</guid>
      <dc:creator>ejarquejm</dc:creator>
      <dc:date>2020-01-08T09:56:59Z</dc:date>
    </item>
  </channel>
</rss>

