<?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: Run macro on a list of values from another dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-on-a-list-of-values-from-another-dataset/m-p/780984#M248893</link>
    <description>Might be a bit memory intensive for a full cartesian join in SQL, but hardly challenging for an array based approach in a data step</description>
    <pubDate>Thu, 18 Nov 2021 10:32:32 GMT</pubDate>
    <dc:creator>Peter_C</dc:creator>
    <dc:date>2021-11-18T10:32:32Z</dc:date>
    <item>
      <title>Run macro on a list of values from another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-on-a-list-of-values-from-another-dataset/m-p/780976#M248890</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have two datasets. The main dataset contains start year and end year for a list of IDs and I need to&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) identify for each ID if the person is present in a list of years I'm interested (thus the 2nd dataset) interest, and&lt;/P&gt;
&lt;P&gt;2) then aggregate by year to count the number of persons present in a given year.&lt;/P&gt;
&lt;P&gt;Step 1 is the one I have trouble with.&lt;/P&gt;
&lt;P&gt;The example here is a simplified version: 1) the actual data has more than 100K cases, 2) the start and end points are actually in dates, not year, and 3) the list of years of interest has more than 150 date values. So I want to bring in those values from an external dataset (and run one at a time?)&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; input id startyr endyr;
datalines;
1 2015 2017
2 2019 2020
3 2017 2020
4 2018 2019
5 2019 2019
6 2017 2021
7 2020 2020
;
data year; input year;
datalines;
2016
2017
2019
2020
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I'm to write code one year at a time, then here it is, to pull a list of IDs whose start and end year fall between my year of interest (step 1)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro get (year);
proc sql; create table year2020 as select id, startyr, endyr
from have where startyr &amp;lt;= 2016 &amp;lt;= endyr;
quit;
%get(2016);
%get(2017);
%get(2020);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for&lt;/P&gt;
&lt;P&gt;with year= 2016, I'd pull out ID 1&lt;/P&gt;
&lt;P&gt;with year= 2017, I'd pull out ID 1,3,6&lt;/P&gt;
&lt;P&gt;with year= 2020, I'd pull out ID 2,3,6,7&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ultimately I'll aggregate each data to count and have my WANT data by year as the following:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Year&amp;nbsp; Count
2016&amp;nbsp;&amp;nbsp; 1
2017&amp;nbsp;&amp;nbsp; 3
2020&amp;nbsp;&amp;nbsp; 4
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks in advance for any tips you can give.&lt;/P&gt;</description>
      <pubDate>Thu, 18 Nov 2021 09:28:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-macro-on-a-list-of-values-from-another-dataset/m-p/780976#M248890</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2021-11-18T09:28:28Z</dc:date>
    </item>
    <item>
      <title>Re: Run macro on a list of values from another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-on-a-list-of-values-from-another-dataset/m-p/780984#M248893</link>
      <description>Might be a bit memory intensive for a full cartesian join in SQL, but hardly challenging for an array based approach in a data step</description>
      <pubDate>Thu, 18 Nov 2021 10:32:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-macro-on-a-list-of-values-from-another-dataset/m-p/780984#M248893</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2021-11-18T10:32:32Z</dc:date>
    </item>
    <item>
      <title>Re: Run macro on a list of values from another dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Run-macro-on-a-list-of-values-from-another-dataset/m-p/780986#M248894</link>
      <description>&lt;P&gt;No need for a macro at all:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table id_year as
  select have.id, year.year
  from have left join year
  on have.startyr le year.year le have.endyr
;
create table want as
  select year, count(id) as count
  from id_year
  group by year
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Nov 2021 10:37:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Run-macro-on-a-list-of-values-from-another-dataset/m-p/780986#M248894</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-11-18T10:37:49Z</dc:date>
    </item>
  </channel>
</rss>

