<?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: Person Count for several years in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/570994#M12066</link>
    <description>&lt;P&gt;If you show your input data and expected output you can likely get a better answer. Yes, there are ways to simplify that code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/164229"&gt;@Dynamike&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How can i simplify this code (macro or loop):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc SQL;&lt;BR /&gt;Create Table Test as select distinct&lt;BR /&gt;Count(distinct case when 2005 GE year(start) and 2005 LE year(end) then person else . end) as person_count_2005&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2006 GE year(start) and 2006 LE year(end) then person else . end) as person_count_2006&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2007 GE year(start) and 2007 LE year(end) then person else . end) as person_count_2007&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2008 GE year(start) and 2008 LE year(end) then person else . end) as person_count_2008&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2009 GE year(start) and 2009 LE year(end) then person else . end) as person_count_2009&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2010GE year(start) and 2010 LE year(end) then person else . end) as person_count_2010&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2011 GE year(start) and 2011 LE year(end) then person else . end) as person_count_2011&lt;BR /&gt;from data;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is example only for the years 2005 to 2011, but i want count person for more years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your effort!&lt;/P&gt;
&lt;P&gt;Dynamike&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jul 2019 15:04:39 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-07-03T15:04:39Z</dc:date>
    <item>
      <title>Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/570952#M12061</link>
      <description>&lt;P&gt;How can i simplify this code (macro or loop):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc SQL;&lt;BR /&gt;Create Table Test as select distinct&lt;BR /&gt;Count(distinct case when 2005 GE year(start) and 2005 LE year(end) then person else . end) as person_count_2005&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2006 GE year(start) and 2006 LE year(end) then person else . end) as person_count_2006&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2007 GE year(start) and 2007 LE year(end) then person else . end) as person_count_2007&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2008 GE year(start) and 2008 LE year(end) then person else . end) as person_count_2008&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2009 GE year(start) and 2009 LE year(end) then person else . end) as person_count_2009&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2010GE year(start) and 2010 LE year(end) then person else . end) as person_count_2010&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2011 GE year(start) and 2011 LE year(end) then person else . end) as person_count_2011&lt;BR /&gt;from data;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is example only for the years 2005 to 2011, but i want count person for more years.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your effort!&lt;/P&gt;&lt;P&gt;Dynamike&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 13:16:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/570952#M12061</guid>
      <dc:creator>Dynamike</dc:creator>
      <dc:date>2019-07-03T13:16:28Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/570970#M12063</link>
      <description>&lt;P&gt;does this code work and give you the expected results for each person and year how you currently have it?&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 13:57:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/570970#M12063</guid>
      <dc:creator>VDD</dc:creator>
      <dc:date>2019-07-03T13:57:50Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/570994#M12066</link>
      <description>&lt;P&gt;If you show your input data and expected output you can likely get a better answer. Yes, there are ways to simplify that code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/164229"&gt;@Dynamike&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;How can i simplify this code (macro or loop):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc SQL;&lt;BR /&gt;Create Table Test as select distinct&lt;BR /&gt;Count(distinct case when 2005 GE year(start) and 2005 LE year(end) then person else . end) as person_count_2005&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2006 GE year(start) and 2006 LE year(end) then person else . end) as person_count_2006&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2007 GE year(start) and 2007 LE year(end) then person else . end) as person_count_2007&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2008 GE year(start) and 2008 LE year(end) then person else . end) as person_count_2008&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2009 GE year(start) and 2009 LE year(end) then person else . end) as person_count_2009&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2010GE year(start) and 2010 LE year(end) then person else . end) as person_count_2010&lt;BR /&gt;,&lt;BR /&gt;Count(distinct case when 2011 GE year(start) and 2011 LE year(end) then person else . end) as person_count_2011&lt;BR /&gt;from data;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is example only for the years 2005 to 2011, but i want count person for more years.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for your effort!&lt;/P&gt;
&lt;P&gt;Dynamike&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 15:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/570994#M12066</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-03T15:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571004#M12067</link>
      <description>&lt;P&gt;Hi thank you for your answer and sorry for not showing input and output data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Input data looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Person &amp;nbsp;&amp;nbsp; start &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; end&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 02/01/2006 &amp;nbsp; &amp;nbsp; &amp;nbsp; 30/07/2009&lt;/P&gt;&lt;P&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 04/04/2009 &amp;nbsp; &amp;nbsp;&amp;nbsp; 30/11/2011&lt;/P&gt;&lt;P&gt;3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 05/04/2007 &amp;nbsp; &amp;nbsp; &amp;nbsp; 03/02/2011&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output data should look like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;year &amp;nbsp; &amp;nbsp; Count&lt;/P&gt;&lt;P&gt;2005 &amp;nbsp; &amp;nbsp; 0&lt;/P&gt;&lt;P&gt;2006 &amp;nbsp; &amp;nbsp; 1&lt;/P&gt;&lt;P&gt;2007 &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;2008 &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;2009 &amp;nbsp; &amp;nbsp; 3&lt;/P&gt;&lt;P&gt;2010 &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;2011 &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OR if this is not possible:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Count_2005 &amp;nbsp; Count_2006&amp;nbsp; Count_2007&amp;nbsp; Count_2008 ….&lt;/P&gt;&lt;P&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 15:42:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571004#M12067</guid>
      <dc:creator>Dynamike</dc:creator>
      <dc:date>2019-07-03T15:42:08Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571006#M12068</link>
      <description>&lt;P&gt;Thank you for your answer.&lt;/P&gt;&lt;P&gt;The code does not work properly&lt;/P&gt;&lt;P&gt;See the reply to Reeza for more Information, what the result should look like.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 15:44:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571006#M12068</guid>
      <dc:creator>Dynamike</dc:creator>
      <dc:date>2019-07-03T15:44:15Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571007#M12069</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/164229"&gt;@Dynamike&lt;/a&gt;&amp;nbsp;Below code has significant room for improvement but it should be able to get the job done. Assuming your dataset is called have ..else edit in the SQL statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA MasterDataSet;
	FORMAT Year 8. PersonCount 8.;
	STOP;
RUN;

%MACRO CalcTotalForYear(YearStart, NumYearsAhead);
	%DO YEAR_Num=0 %TO &amp;amp;NumYearsAhead;

		PROC SQL;
			CREATE TABLE TempDataSet AS SELECT &amp;amp;YearStart+&amp;amp;Year_Num AS Year, COUNT(*) AS 
				PersonCount FROM have WHERE YEAR(Start)&amp;lt;=&amp;amp;YEARStart+&amp;amp;Year_Num AND 
				YEAR(END)&amp;gt;=&amp;amp;YEARStart+&amp;amp;Year_Num;
		QUIT;
		DATA MasterDataSet;
			SET MasterDataSet TempDataSet;
		RUN;

	%END;
%MEND;

/* Change below for starting year and number of years ahead */
%CalcTotalForYear(2005, 7);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Ultimately it should give a MasterDataSet. Please let me know if it worked.&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 15:48:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571007#M12069</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-07-03T15:48:17Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571010#M12070</link>
      <description>&lt;P&gt;With the code above, I got the following output.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Output" style="width: 164px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30728i7794C8CBF88888B7/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot from 2019-07-03 21-24-11.png" alt="Output" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Output&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I used the macro as&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%CalcTotalForYear(2005, 6);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;As said before, I have assumed that the dataset you have is conventionally named as "have"&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2019 15:56:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571010#M12070</guid>
      <dc:creator>koyelghosh</dc:creator>
      <dc:date>2019-07-03T15:56:39Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571011#M12071</link>
      <description>Does everyone have an end date? How many years are you expecting?</description>
      <pubDate>Wed, 03 Jul 2019 16:00:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571011#M12071</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-03T16:00:04Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571012#M12072</link>
      <description>You could execute the file if you also have Stata on the machine. It's the equivalent of calling the do file via command line or in batch, but not like running it in Stata. &lt;BR /&gt;&lt;BR /&gt;Beyond that I haven't seen very much integration with Stata from SAS.</description>
      <pubDate>Wed, 03 Jul 2019 16:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571012#M12072</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-03T16:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571014#M12073</link>
      <description>&lt;P&gt;That sees like a reasonable way to do it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can easily generate that code with a %DO loop inside of a macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro year_count(in=,out=,start=,end=);
%local year sep;
proc sql noprint;
  create table &amp;amp;out as select
%do year=&amp;amp;start %to &amp;amp;end;
  &amp;amp;sep. count(distinct case when (&amp;amp;year between year(start) and year(end)) then person end)
    as person_count_&amp;amp;year
  %let sep=,;
%end;
  from &amp;amp;in
;
quit;
%mend year_count;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So to recreate your example use a call like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%year_count(in=data,out=test,start=2005,end=2011);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jul 2019 16:09:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571014#M12073</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-03T16:09:22Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571018#M12075</link>
      <description>&lt;P&gt;It might be easier to generate a dataset with all of the years (intervals) that you want to find counts for.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data years;
 do year=2005 to 2011 ;
   firstday=mdy(1,1,year);
   lastday=mdy(12,31,year);
   output;
 end;
 format firstday lastday yymmdd10.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then join that with the data and generate the counts.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;
  create table tall as
  select year,count(distinct person) as count
  from years a
  left join have b
  on a.firstday &amp;lt;= b.end and a.lastday &amp;gt;= b.start
  group by year
  order by year
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 03 Jul 2019 16:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571018#M12075</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-03T16:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571053#M12088</link>
      <description>Don't you usually need to account for how long a person is present within a year as well? Ie if you join in July 1, you're only 0.5 person year, not a full person year.</description>
      <pubDate>Wed, 03 Jul 2019 18:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571053#M12088</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-07-03T18:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: Person Count for several years</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571126#M12094</link>
      <description>Thank you for your advice. Counting the person within a year is only the first step.&lt;BR /&gt;Do you know how to add the sum of days per year for all person (which i can then divide easily by 365 days)&lt;BR /&gt;&lt;BR /&gt;Thank you for your effort.&lt;BR /&gt;</description>
      <pubDate>Thu, 04 Jul 2019 04:38:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Person-Count-for-several-years/m-p/571126#M12094</guid>
      <dc:creator>Dynamike</dc:creator>
      <dc:date>2019-07-04T04:38:19Z</dc:date>
    </item>
  </channel>
</rss>

