<?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 How to caculate age range between youngest and oldest in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/How-to-caculate-age-range-between-youngest-and-oldest/m-p/376347#M3034</link>
    <description>&lt;P&gt;I need to caculate age range between youngest and oldest of each type, could someone help mw with this as not every row has data.&lt;/P&gt;&lt;PRE&gt;proc sort data=hw5.customer_club out=work.customer_club;
   by Customer_Type Customer_BirthDate;
   format Customer_BirthDate mmddyy10.;
run;

data work.Oldest;
   set work.customer_club;
   by Customer_Type Customer_BirthDate;
   if First.Customer_Type then oldest=Customer_ID;
      oldest_bday=Customer_BirthDate;
   if First.Customer_Type;
   keep oldest oldest_bday Customer_Type;
   format oldest_bday mmddyy10.;
run;

data work.Youngest;
   set work.customer_club;
   by Customer_Type Customer_BirthDate;
   if Last.Customer_Type then youngest=Customer_ID;
      youngest_bday=Customer_BirthDate;
   if Last.Customer_Type;
   keep youngest youngest_bday Customer_Type;
   format youngest_bday mmddyy10.;
run;

proc sort data=work.Youngest out=new_Youngest;
   by Customer_Type;
run;

proc sort data=work.Oldest out=new_Oldest;
   by Customer_Type;
run;

data work.merge1;
   set work.new_Youngest work.new_Oldest;
   by Customer_Type;
run;&lt;/PRE&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10350iC5B8600B1DCBF39D/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="微信图片_20170716235613.png" title="微信图片_20170716235613.png" /&gt;&lt;/P&gt;&lt;P&gt;The output of merge1 is above. I need to caculate their age range:&amp;nbsp;agerange = the number of years (rounded to the nearest tenth) between the age of the oldest and youngest members of that type&lt;/P&gt;</description>
    <pubDate>Sun, 16 Jul 2017 15:58:23 GMT</pubDate>
    <dc:creator>ydu180</dc:creator>
    <dc:date>2017-07-16T15:58:23Z</dc:date>
    <item>
      <title>How to caculate age range between youngest and oldest</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-to-caculate-age-range-between-youngest-and-oldest/m-p/376347#M3034</link>
      <description>&lt;P&gt;I need to caculate age range between youngest and oldest of each type, could someone help mw with this as not every row has data.&lt;/P&gt;&lt;PRE&gt;proc sort data=hw5.customer_club out=work.customer_club;
   by Customer_Type Customer_BirthDate;
   format Customer_BirthDate mmddyy10.;
run;

data work.Oldest;
   set work.customer_club;
   by Customer_Type Customer_BirthDate;
   if First.Customer_Type then oldest=Customer_ID;
      oldest_bday=Customer_BirthDate;
   if First.Customer_Type;
   keep oldest oldest_bday Customer_Type;
   format oldest_bday mmddyy10.;
run;

data work.Youngest;
   set work.customer_club;
   by Customer_Type Customer_BirthDate;
   if Last.Customer_Type then youngest=Customer_ID;
      youngest_bday=Customer_BirthDate;
   if Last.Customer_Type;
   keep youngest youngest_bday Customer_Type;
   format youngest_bday mmddyy10.;
run;

proc sort data=work.Youngest out=new_Youngest;
   by Customer_Type;
run;

proc sort data=work.Oldest out=new_Oldest;
   by Customer_Type;
run;

data work.merge1;
   set work.new_Youngest work.new_Oldest;
   by Customer_Type;
run;&lt;/PRE&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/10350iC5B8600B1DCBF39D/image-size/original?v=1.0&amp;amp;px=-1" border="0" alt="微信图片_20170716235613.png" title="微信图片_20170716235613.png" /&gt;&lt;/P&gt;&lt;P&gt;The output of merge1 is above. I need to caculate their age range:&amp;nbsp;agerange = the number of years (rounded to the nearest tenth) between the age of the oldest and youngest members of that type&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jul 2017 15:58:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-to-caculate-age-range-between-youngest-and-oldest/m-p/376347#M3034</guid>
      <dc:creator>ydu180</dc:creator>
      <dc:date>2017-07-16T15:58:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to caculate age range between youngest and oldest</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-to-caculate-age-range-between-youngest-and-oldest/m-p/376354#M3035</link>
      <description>&lt;P&gt;At a minimum, the final step needs to switch from SET to MERGE. &amp;nbsp;That would combine information onto a single observation for each customer type.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The calculations are easy:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;age_range_in_years = (oldest_bday - youngest_bday) / 365.25;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There is no need to round this to the nearest tenth. &amp;nbsp;You would lose a little accuracy, so the proper way to control this would be when printing. &amp;nbsp;Apply a format to print to the nearest tenth:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;format age_range_in_years 5.1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note, this result can be gotten much more easily if all you need is this one number per customer type:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc summary data=hw5.customer_club nway;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;class customer_type;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;var customer_birthdate;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;output out=ages (keep=customer_type youngest_age oldest_age) min=youngest_age max=oldest_age;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need the customer id as well, that too can be done with a short program.&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jul 2017 17:13:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-to-caculate-age-range-between-youngest-and-oldest/m-p/376354#M3035</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-07-16T17:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to caculate age range between youngest and oldest</title>
      <link>https://communities.sas.com/t5/SAS-Studio/How-to-caculate-age-range-between-youngest-and-oldest/m-p/376355#M3036</link>
      <description>&lt;P&gt;I think it would be easiest with proc sql. e.g.:&lt;/P&gt;
&lt;PRE&gt;libname hw5 '/folders/myfolders';
data hw5.customer_club;
  informat Customer_BirthDate date9.;
  input Customer_Type $ Customer_BirthDate;
  cards;
AAA 20JUN1954
AAA 17APR1992
AAA 14JUL1960
BBB 18JUN1961
BBB 13FEB1995
BBB 6MAY1965
;
proc sql noprint;
  create table merge1 as
    select min(Customer_BirthDate) as youngest format=date9.,
           max(Customer_BirthDate) as oldeest format=date9.,
           yrdif(min(Customer_BirthDate), max(Customer_BirthDate)) as Range format=4.1
      from hw5.customer_club
        group by Customer_Type
  ;
quit;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Jul 2017 17:24:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/How-to-caculate-age-range-between-youngest-and-oldest/m-p/376355#M3036</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-07-16T17:24:53Z</dc:date>
    </item>
  </channel>
</rss>

