<?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 Macro to calculate age as of each year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859065#M339425</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a year character variable (2014, 2015, 2016... 2022) and I want to write a macro that calculates age as of the end of each year so that i do not need to hard code it every single time like below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe this should be a fairly easy macro, can anyone help out? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data edd_sc_cder; set edd_sc_cder;
	cy=substr(edd_qrt, 1,4);
	if cy="2014" then age=floor((intck('month',birthday,'31dec2014'd)-(day('31dec2014'd) &amp;lt; day(birthday)))/12); 
		else if fy="2015" then age=floor((intck('month',birthday,'31dec2015'd)-(day('31dec2015'd) &amp;lt; day(birthday)))/12); 
		else if fy="2016" then age=floor((intck('month',birthday,'31dec2016'd)-(day('31dec2016'd) &amp;lt; day(birthday)))/12); 
		else if fy="2017" then age=floor((intck('month',birthday,'31dec2017'd)-(day('31dec2017'd) &amp;lt; day(birthday)))/12); 
		else if fy="2018" then age=floor((intck('month',birthday,'31dec2018'd)-(day('31dec2018'd) &amp;lt; day(birthday)))/12); 
		else if fy="2019" then age=floor((intck('month',birthday,'31dec2019'd)-(day('31dec2019'd) &amp;lt; day(birthday)))/12); 
		else if fy="2020" then age=floor((intck('month',birthday,'31dec2020'd)-(day('31dec2020'd) &amp;lt; day(birthday)))/12); 
		else if fy="2021" then age=floor((intck('month',birthday,'31dec2021'd)-(day('31dec2021'd) &amp;lt; day(birthday)))/12); 
		else if fy="2022" then age=floor((intck('month',birthday,'31dec2022'd)-(day('31dec2022'd) &amp;lt; day(birthday)))/12); 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 15 Feb 2023 21:24:18 GMT</pubDate>
    <dc:creator>kevsma</dc:creator>
    <dc:date>2023-02-15T21:24:18Z</dc:date>
    <item>
      <title>Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859065#M339425</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a year character variable (2014, 2015, 2016... 2022) and I want to write a macro that calculates age as of the end of each year so that i do not need to hard code it every single time like below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe this should be a fairly easy macro, can anyone help out? Thanks!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data edd_sc_cder; set edd_sc_cder;
	cy=substr(edd_qrt, 1,4);
	if cy="2014" then age=floor((intck('month',birthday,'31dec2014'd)-(day('31dec2014'd) &amp;lt; day(birthday)))/12); 
		else if fy="2015" then age=floor((intck('month',birthday,'31dec2015'd)-(day('31dec2015'd) &amp;lt; day(birthday)))/12); 
		else if fy="2016" then age=floor((intck('month',birthday,'31dec2016'd)-(day('31dec2016'd) &amp;lt; day(birthday)))/12); 
		else if fy="2017" then age=floor((intck('month',birthday,'31dec2017'd)-(day('31dec2017'd) &amp;lt; day(birthday)))/12); 
		else if fy="2018" then age=floor((intck('month',birthday,'31dec2018'd)-(day('31dec2018'd) &amp;lt; day(birthday)))/12); 
		else if fy="2019" then age=floor((intck('month',birthday,'31dec2019'd)-(day('31dec2019'd) &amp;lt; day(birthday)))/12); 
		else if fy="2020" then age=floor((intck('month',birthday,'31dec2020'd)-(day('31dec2020'd) &amp;lt; day(birthday)))/12); 
		else if fy="2021" then age=floor((intck('month',birthday,'31dec2021'd)-(day('31dec2021'd) &amp;lt; day(birthday)))/12); 
		else if fy="2022" then age=floor((intck('month',birthday,'31dec2022'd)-(day('31dec2022'd) &amp;lt; day(birthday)))/12); 
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Feb 2023 21:24:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859065#M339425</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-02-15T21:24:18Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859067#M339427</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/379997"&gt;@kevsma&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I have a year &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;character&lt;/STRONG&gt; &lt;/FONT&gt;variable (2014, 2015, 2016... 2022) &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want to "caluculate" then use numeric variables. Even if you just drop it later. Then you can use MDY(12,31,numyear) to get the DATE of a specific day of the year.&lt;/P&gt;
&lt;P&gt;Try looking at the YRDIF function&lt;/P&gt;
&lt;PRE&gt;age = floor( yrdif(birthday,mdy(12,31,input(fy,4.))));&lt;/PRE&gt;
&lt;P&gt;Input turns the character value into a numeric. The function MDY returns a date, in this case the last day of a calendar year.&lt;/P&gt;
&lt;P&gt;YRDIF by default returns an age with a decimal fraction given a start date and end date.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 21:53:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859067#M339427</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-15T21:53:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859068#M339428</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; will convert it to numeric. Any suggestions as how to write the macro? Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 21:59:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859068#M339428</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-02-15T21:59:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859069#M339429</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/379997"&gt;@kevsma&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp; will convert it to numeric. Any suggestions as how to write the macro? Thank you!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;No macro needed, it is a numeric formula. Did you try the code shown in place of your "if/then" block of code with your data?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 22:02:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859069#M339429</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2023-02-15T22:02:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859075#M339433</link>
      <description>&lt;P&gt;Assuming the change from cy to fy is a typo, you can modify your code to be dynamic as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data edd_sc_cder; 
set edd_sc_cder;
	cy=input(substr(edd_qrt, 1,4), 8.);
         index_date = mdy(12, 31, cy);
         format index_date date9.;
	 age=floor((intck('month',birthday,index_date)-(day(index_date) &amp;lt; day(birthday)))/12); 
		
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could simplify this further, embedding the date calculation into the code but this makes it clear that you're always calculating age as of the last day of the year.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Usually July1st (mid year is used) but I'm assuming you have a reason for doing it as of end of year &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 22:33:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859075#M339433</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2023-02-15T22:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859077#M339435</link>
      <description>&lt;P&gt;I don't understand why you're calculating fy and using it in IF-ELSE statement. I believe there would be some good reason but I can't tell without knowing an input data and no understanding exact requirement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Though it seems you're only interested in current FY year to calculate age. If so then you can have only one following IF statement. You just need to have dynamic current year to compare with your FY. And replace hard-coded last day of the year date with dynamic date using&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;intnx('year',today(),0,'E')&lt;/STRONG&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;if fy = year(today()) then age = floor((intck('month',birthday,intnx('year',today(),0,'E'))-(day(intnx('year',today(),0,'E')) &amp;lt; day(birthday)))/12);&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In case if you have complex input dataset and want to use sas macro then here one simple example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Macro that calculate age */
%macro age(date, birth);
	floor((intck('month', &amp;amp;birth, &amp;amp;date) - (day(&amp;amp;date) &amp;lt; day(&amp;amp;birth))) / 12) 
	
%mend age;

data _NULL_;
	Birthday='11NOV1990'd;

	/* i assume you have birthday date variable in the input data set hence can be easily replaced */
	fy=2023;

	/* i assume you have fy variable in the input data set hence can be easily replaced */
	LastDayOftheYear=intnx('year', today(), 0, 'E');

	if fy=year(today()) then
		age=%age(LastDayOftheYear, Birthday);
	put Birthday=date9. LastDayOftheYear=date9.;
	put age=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 22:36:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859077#M339435</guid>
      <dc:creator>MayurJadhav</dc:creator>
      <dc:date>2023-02-15T22:36:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859241#M339494</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;this works perfectly!!&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2023 17:37:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859241#M339494</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-02-16T17:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859242#M339495</link>
      <description>&lt;P&gt;Thanks for the input, I guess my main question is if I have a longitudinal dataset with a time variable (fy, cy, quarter, etc), how to grab the age by the end of each time period? It looks like trying to use a macro is over-complicating the issue.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2023 17:43:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859242#M339495</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-02-16T17:43:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro to calculate age as of each year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859246#M339497</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/379997"&gt;@kevsma&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks for the input, I guess my main question is if I have a longitudinal dataset with a time variable (fy, cy, quarter, etc), how to grab the age by the end of each time period? It looks like trying to use a macro is over-complicating the issue.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Macro logic is used to manipulate text, for example to generate SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To work with actual data, like the value of a FY or CY variable, use actual SAS code.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2023 18:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-to-calculate-age-as-of-each-year/m-p/859246#M339497</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-02-16T18:05:25Z</dc:date>
    </item>
  </channel>
</rss>

