<?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: dates subtraction in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612156#M178554</link>
    <description>&lt;P&gt;Is startdate a numeric variable or a character variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is randdate a numeric variable or a character variable?&lt;/P&gt;</description>
    <pubDate>Mon, 16 Dec 2019 19:10:51 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-12-16T19:10:51Z</dc:date>
    <item>
      <title>dates subtraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612153#M178552</link>
      <description>&lt;P&gt;Following table - i would like to subtract randdate and start date and convert into "Day" but if randdate = startdate then i want that time to be Day 1, rather than day 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;subjectid&lt;/TD&gt;&lt;TD&gt;startdate&lt;/TD&gt;&lt;TD&gt;randdate&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/2/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/3/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/4/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Something like this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;subjectid&lt;/TD&gt;&lt;TD&gt;startdate&lt;/TD&gt;&lt;TD&gt;randdate&lt;/TD&gt;&lt;TD&gt;group&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;Day 1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/2/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;Day 2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/2/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;Day 2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;1/3/2019&lt;/TD&gt;&lt;TD&gt;1/1/2019&lt;/TD&gt;&lt;TD&gt;Day 3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Right now i can only do randdate - startdate and recode if subtract=0 then group = "day 1" but this is tedious&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:01:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612153#M178552</guid>
      <dc:creator>radhikaa4</dc:creator>
      <dc:date>2019-12-16T19:01:01Z</dc:date>
    </item>
    <item>
      <title>Re: dates subtraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612156#M178554</link>
      <description>&lt;P&gt;Is startdate a numeric variable or a character variable?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is randdate a numeric variable or a character variable?&lt;/P&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:10:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612156#M178554</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-16T19:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: dates subtraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612174#M178555</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/153469"&gt;@radhikaa4&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution you described is the easiest.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also define your own function using proc fmcp. It is easier to maintain if the rule is going to change in the future and if you use it in multiple datasets.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Function definition -&amp;gt; Fcalc_Day() with 2 arguments as defined below */

libname lib_fcmp "&amp;amp;path."; /* to be specified */

proc fcmp outlib=lib_fcmp.functions.dev;
	function Fcalc_Day(randdate, startdate);
	
	if randdate &amp;gt; startdate then return (startdate - randdate);
	if randdate &amp;lt;= startdate then return (startdate - randdate + 1);
	
	endsub;
quit;

options cmplib=lib_fcmp.functions;

/* Use case */
data have;
	input subjectid	startdate : mmddyy10. randdate : mmddyy10.;
	format startdate randdate mmddyy10.;
	cards;
1 12/31/2018 1/1/2019
1 1/1/2019 1/1/2019
1 1/2/2019 1/1/2019
1 1/3/2019 1/1/2019
1 1/4/2019 1/1/2019
;
run;

data want;
	set have;
	day = Fcalc_Day(randdate,startdate);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 19:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612174#M178555</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2019-12-16T19:23:45Z</dc:date>
    </item>
    <item>
      <title>Re: dates subtraction</title>
      <link>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612216#M178585</link>
      <description>&lt;P&gt;The "tedious" way is fine, but you can see if you find this more appealing:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;days = startdate - randdate + (startdate &amp;gt;= randdate);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 16 Dec 2019 21:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/dates-subtraction/m-p/612216#M178585</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-12-16T21:08:45Z</dc:date>
    </item>
  </channel>
</rss>

