<?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 Calculate number of days from more than one variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/607921#M176825</link>
    <description>&lt;P&gt;I want to create a new variable that calculates the number of days between hospital admission date (adate) and the last time a person received a vaccine (vdate) --vaccination date being before admission date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that a person can have up to 12 vaccinations, so vdate is actually 12 variables: from vdate_1 up to vdate_12 (arranged from oldest to newest).&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate the number of days based on each individual's LAST vaccination date regardless the number of vaccines that person took.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what the excel sheet looks like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 838px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34300iD90352DFED90901D/image-dimensions/838x141?v=v2" width="838" height="141" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
    <pubDate>Thu, 28 Nov 2019 00:51:48 GMT</pubDate>
    <dc:creator>Maiio</dc:creator>
    <dc:date>2019-11-28T00:51:48Z</dc:date>
    <item>
      <title>Calculate number of days from more than one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/607921#M176825</link>
      <description>&lt;P&gt;I want to create a new variable that calculates the number of days between hospital admission date (adate) and the last time a person received a vaccine (vdate) --vaccination date being before admission date.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that a person can have up to 12 vaccinations, so vdate is actually 12 variables: from vdate_1 up to vdate_12 (arranged from oldest to newest).&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to calculate the number of days based on each individual's LAST vaccination date regardless the number of vaccines that person took.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is what the excel sheet looks like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 838px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/34300iD90352DFED90901D/image-dimensions/838x141?v=v2" width="838" height="141" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2019 00:51:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/607921#M176825</guid>
      <dc:creator>Maiio</dc:creator>
      <dc:date>2019-11-28T00:51:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of days from more than one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/607938#M176833</link>
      <description>&lt;P&gt;Here you go, I did it with cascading conditions&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as 
select t1.*,
(case when t1.vdate_12 not is null then t1.vdate_12 
	else ( case when t1.vdate_11 not is null then t1.vdate_11 
		else ( case when t1.vdate_10 not is null then t1.vdate_10
			else ( case when t1.vdate_9 not is null then t1.vdate_9
				else ( case when t1.vdate_8 not is null then t1.vdate_8
					else ( case when t1.vdate_7 not is null then t1.vdate_7
						else ( case when t1.vdate_6 not is null then t1.vdate_6
							else ( case when t1.vdate_5 not is null then t1.vdate_5
								else ( case when t1.vdate_4 not is null then t1.vdate_4
									else ( case when t1.vdate_3 not is null then t1.vdate_3
										else ( case when t1.vdate_2 not is null then t1.vdate_2
											else ( case when t1.vdate_1 not is null then t1.vdate_1 else . end)
											   end) end) end) end) end) end) end) end) end) end) end) as Last_Vaccine_Date format=date11.,
(t1.ADate-(calculated Last_Vaccine_Date)) as Day_Count
from have t1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Next time please post your data as code, instructions &lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_self"&gt;here&lt;/A&gt;. This helps the person helping you.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2019 02:49:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/607938#M176833</guid>
      <dc:creator>sustagens</dc:creator>
      <dc:date>2019-11-28T02:49:00Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of days from more than one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/607956#M176839</link>
      <description>&lt;P&gt;Assuming your dates are imported as proper SAS dates :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;days = intck("day", adate, max(of vdate_1-vdate_12));&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 28 Nov 2019 05:01:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/607956#M176839</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-11-28T05:01:39Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of days from more than one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/608082#M176913</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You don't need to add an else clause after each when. You can chain the when as follows :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;           case when t1.vdate_12 not is null then t1.vdate_12
                when t1.vdate_11 not is null then t1.vdate_11
                ...
                when t1.vdate_1 not is null then t1.vdate_1 
           else . end as Last_Vaccine_Date format=date11.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;=&amp;gt; Each when is evaluated only if the predeeding conditions have not been satisfied yet.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The coalesce function can alos be used here for a more concise expression :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;select t1.*, coalesce(t1.vdate_12, t1.vdate_11, ..., t1.vdate_1) as ...&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Nov 2019 16:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/608082#M176913</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-11-28T16:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of days from more than one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/608084#M176914</link>
      <description>&lt;P&gt;Why would you post a photograph of data? This forum's editor supports copy and paste of text. You can even paste in text from Excel.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So is the rule this:&lt;/P&gt;
&lt;P&gt;find the max vaccination date that is less than (or equal to?) admission date and then take the difference?&lt;/P&gt;
&lt;P&gt;Assuming the VDATE_xxx values are in order like you photograph seems to indicate you could use something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have;
  array vdates vdate_1-vdate_12 ;
  do index=1 to dim(vdates) while (not missing(vdates[index] and vdates[index] &amp;lt; adate);
     days = adate - vdates[index];
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Nov 2019 16:27:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/608084#M176914</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-11-28T16:27:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate number of days from more than one variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/608085#M176915</link>
      <description>&lt;P&gt;The coalesce function is also avaliable in data steps :&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    format vdate_1-vdate_12 last_non_null_date ddmmyy10.;
    vdate_1='11May2009'd;
    vdate_2='08Aug2006'd;
    vdate_3='02Dec2006'd;

    array dt vdate_12-vdate_1;

    last_non_null_date=coalesce(of dt(*));
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Nov 2019 16:27:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-number-of-days-from-more-than-one-variable/m-p/608085#M176915</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2019-11-28T16:27:54Z</dc:date>
    </item>
  </channel>
</rss>

