<?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: Problems with If, then, else -statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412994#M101034</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172388"&gt;@hovliza&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was wondering what I am doing wrong. I want to make a starting point of my follow-up (date_start1) and I have different dates of measurement (date1, date2, date3 etc.) and I want to pick the date which is nearest to the date of questionnaire completion (dataq) as my&amp;nbsp;date_start1. Most of the time date_start1 is date1. But sometimes date2-4 are closer to the date of questionnaire completion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
SET have;
date_start1=date1;
IF abs((date1-dataq)/365.25) GT abs((date2-dataq)/365.25) THEN date_start1=date2;
ELSE IF abs((date1-dataq)/365.25) OR abs((date2-dataq)/365.25) GT abs((date3-dataq)/365.25) THEN date_start1=date3;
ELSE IF abs((date1-dataq)/365.25) OR abs((date2-dataq)/365.25) OR abs((date3-dataq)/365.25) GT abs((date4-dataq)/365.25) THEN date_start1=date4;
FORMAT date_start1 DATE9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't get good results. SAS doesn't pick this:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_start1=date1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;It seems SAS only picks:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF abs((date1-dataq)/365.25) GT abs((date2-dataq)/365.25) THEN date_start1=date2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What should I do?&lt;/P&gt;
&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is NO way for SAS to have a value of date_start1= date1. (unless date1 matches one of date2, 3 or 4)&lt;/P&gt;
&lt;P&gt;Your if/then/else forces it to have one of the values of date2, date3 or date4.&lt;/P&gt;
&lt;P&gt;Perhaps you meant to have another else that left date_start1 with a value of date1 or a different test at the start before the assignment of date2?&lt;/P&gt;</description>
    <pubDate>Mon, 13 Nov 2017 18:10:54 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2017-11-13T18:10:54Z</dc:date>
    <item>
      <title>Problems with If, then, else -statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412275#M100828</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was wondering what I am doing wrong. I want to make a starting point of my follow-up (date_start1) and I have different dates of measurement (date1, date2, date3 etc.) and I want to pick the date which is nearest to the date of questionnaire completion (dataq) as my&amp;nbsp;date_start1. Most of the time date_start1 is date1. But sometimes date2-4 are closer to the date of questionnaire completion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
SET have;
date_start1=date1;
IF abs((date1-dataq)/365.25) GT abs((date2-dataq)/365.25) THEN date_start1=date2;
ELSE IF abs((date1-dataq)/365.25) OR abs((date2-dataq)/365.25) GT abs((date3-dataq)/365.25) THEN date_start1=date3;
ELSE IF abs((date1-dataq)/365.25) OR abs((date2-dataq)/365.25) OR abs((date3-dataq)/365.25) GT abs((date4-dataq)/365.25) THEN date_start1=date4;
FORMAT date_start1 DATE9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't get good results. SAS doesn't pick this:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_start1=date1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;It seems SAS only picks:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF abs((date1-dataq)/365.25) GT abs((date2-dataq)/365.25) THEN date_start1=date2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What should I do?&lt;/P&gt;
&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 16:00:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412275#M100828</guid>
      <dc:creator>hovliza</dc:creator>
      <dc:date>2017-11-10T16:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: Problems with If, then, else -statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412279#M100829</link>
      <description>&lt;P&gt;Well you could first start by providing some test data in the form of a datastep and what you want the output to look like.&amp;nbsp; At a guess I would say, step one would be to work out the difference in days between each of the dates, then choosing the smallest, e.g:&lt;/P&gt;
&lt;PRE&gt;data have;
  actual="12jan2017"d; date1="01jan2017"d; date2="14jan2017"d; date3="30jan2017"d; output;
  actual="01feb2017"d; date1="01jan2017"d; date2="14jan2017"d; date3="10feb2017"d; output;
  format _all_ date9.;
run;

data want (drop=i);
  set have;
  diff=abs(date1-actual);
  start=date1;
  array v{2} date2 date3;
  do i=1 to 2;
    if abs(v{i}-actual) &amp;lt; diff then do;
      start=v{i};
      diff=abs(v{i}-actual);
    end;
  end;
  format start date9.;
run;
run;
 
&lt;/PRE&gt;
&lt;P&gt;Just to add, I use arrays so its expandable - i.e. you could have date4 or date20, doesn't matter, just update the number.&amp;nbsp; If its only 1 or 2, then maybe if statement for each is fine.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 11:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412279#M100829</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-10T11:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Problems with If, then, else -statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412313#M100838</link>
      <description>&lt;P&gt;Thanks for your suggestion. For my data, the syntax would be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA want (drop=i);&lt;BR /&gt;SET have;&lt;BR /&gt;diff=abs((date1-dateq)/365.25);&lt;BR /&gt;start=date1;&lt;BR /&gt;array v{3} date2 date3 date4;&lt;BR /&gt;do i=1 to 3;&lt;BR /&gt;if abs(v{i}-dateq) &amp;lt; diff then do;&lt;BR /&gt;start=v{i};&lt;BR /&gt;diff=abs(v{i}-dateq);&lt;BR /&gt;END;&lt;BR /&gt;END;&lt;BR /&gt;FORMAT start date9.;&lt;BR /&gt;RUN;&lt;/PRE&gt;&lt;P&gt;But there are no values in my dataset..&amp;nbsp;Whats going wrong?&lt;BR /&gt;&lt;BR /&gt;With this syntax:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  cdatalg="12jan2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="30jan2017"d; datesc4="10nov2017"d; output;
  cdatalg="01feb2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="10feb2017"d; datesc4="10dec2017"d; output;
  format _all_ date9.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I get values in my dataset, but not the closest by the dataq..&amp;nbsp;the dataset looks like;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Dataq&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Date1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Date2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Date3&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Date4&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;Diff&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;start&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;1&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;12JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;14JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;30JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10NOV2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.0301&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;14JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10FEB2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10DEC2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10DEC2017&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;0.0848&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;01JAN2017&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What can I do?&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 13:12:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412313#M100838</guid>
      <dc:creator>hovliza</dc:creator>
      <dc:date>2017-11-10T13:12:41Z</dc:date>
    </item>
    <item>
      <title>Re: Problems with If, then, else -statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412321#M100841</link>
      <description>&lt;P&gt;This is why you should provide test data in the form of a datastep - I cannot see your data!!&amp;nbsp; All I can do is write my own which is what this does:&lt;/P&gt;
&lt;PRE&gt;data have;
  actual="12jan2017"d; date1="01jan2017"d; date2="14jan2017"d; date3="30jan2017"d; output;
  actual="01feb2017"d; date1="01jan2017"d; date2="14jan2017"d; date3="10feb2017"d; output;
  format _all_ date9.;
run;&lt;/PRE&gt;
&lt;P&gt;That gives me data which sounds like the data you have and have not shown me.&amp;nbsp; Using this data I then write the next code:&lt;/P&gt;
&lt;PRE&gt;data want (drop=i);
  set have;
  diff=abs(date1-actual);
  start=date1;
  array v{2} date2 date3;
  do i=1 to 2;
    if abs(v{i}-actual) &amp;lt; diff then do;
      start=v{i};
      diff=abs(v{i}-actual);
    end;
  end;
  format start date9.;
run;&lt;/PRE&gt;
&lt;P&gt;Which runs on the test data I created.&amp;nbsp; You need to modify it to your data&amp;nbsp;&lt;STRONG&gt;which I cannot see as you have not provided it&lt;/STRONG&gt;.&amp;nbsp; If this is your data:&lt;/P&gt;
&lt;PRE&gt;data have;
  cdatalg="12jan2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="30jan2017"d; datesc4="10nov2017"d; output;
  cdatalg="01feb2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="10feb2017"d; datesc4="10dec2017"d; output;
  format _all_ date9.;
run;&lt;/PRE&gt;
&lt;P&gt;Then my step needs to be altered to be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want (drop=i);
  set have;
  diff=abs(datesc1-cdatalg);
  start=datesc1;
  array v{3} datesc2 datesc3 datesc4;
  do i=1 to 3;
    if abs(v{i}-cdatalg) &amp;lt; diff then do;
      start=v{i};
      diff=abs(v{i}-cdatalg);
    end;
  end;
  format start date9.;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 13:39:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412321#M100841</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-10T13:39:23Z</dc:date>
    </item>
    <item>
      <title>Re: Problems with If, then, else -statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412379#M100861</link>
      <description>&lt;P&gt;Yes I understand.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  cdatalg="12jan2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="30jan2017"d; datesc4="10nov2017"d; output;
  cdatalg="01feb2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="10feb2017"d; datesc4="10dec2017"d; output;
  format _all_ date9.;
run;

DATA want (drop=i);
SET have;
diff=abs((datesc1-cdatalg)/365.25);
start=datesc1;
array v{3} datesc2 datesc3 datesc4;
do i=1 to 3;
if abs(v{i}-cdatalg) &amp;lt; diff then do;
start=v{i};
diff=abs(v{i}-cdatalg);
END;
END;
FORMAT start date9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If i am doing this I get the data I've showed in my other reaction. But with only the first date as a start. But that's not right.&amp;nbsp;&lt;BR /&gt;It is not possible for me to show my dataset, because of confidentially reasons..&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 15:16:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412379#M100861</guid>
      <dc:creator>hovliza</dc:creator>
      <dc:date>2017-11-10T15:16:46Z</dc:date>
    </item>
    <item>
      <title>Re: Problems with If, then, else -statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412421#M100873</link>
      <description>&lt;P&gt;Your code showed you creating dataset want, but then analyzing dataset have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, your code does a division by 365.25. Is that necessary?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It appears that you may only need something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  cdatalg="12jan2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="30jan2017"d; datesc4="10nov2017"d; output;
  cdatalg="01feb2017"d; datesc1="01jan2017"d; datesc2="14jan2017"d; datesc3="10feb2017"d; datesc4="10dec2017"d; output;
  format _all_ date9.;
run;

DATA want (drop=i);
  SET have;
  diff=999999;
  array v{*} datesc1-datesc4;
  do i=1 to dim(v);
    if abs(v{i}-cdatalg) &amp;lt; diff then do;
      start=v{i};
      diff=abs(v{i}-cdatalg);
    END;
  END;
  FORMAT start date9.;
RUN;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 16:55:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412421#M100873</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-11-10T16:55:42Z</dc:date>
    </item>
    <item>
      <title>Re: Problems with If, then, else -statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412994#M101034</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/172388"&gt;@hovliza&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was wondering what I am doing wrong. I want to make a starting point of my follow-up (date_start1) and I have different dates of measurement (date1, date2, date3 etc.) and I want to pick the date which is nearest to the date of questionnaire completion (dataq) as my&amp;nbsp;date_start1. Most of the time date_start1 is date1. But sometimes date2-4 are closer to the date of questionnaire completion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I do this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want;
SET have;
date_start1=date1;
IF abs((date1-dataq)/365.25) GT abs((date2-dataq)/365.25) THEN date_start1=date2;
ELSE IF abs((date1-dataq)/365.25) OR abs((date2-dataq)/365.25) GT abs((date3-dataq)/365.25) THEN date_start1=date3;
ELSE IF abs((date1-dataq)/365.25) OR abs((date2-dataq)/365.25) OR abs((date3-dataq)/365.25) GT abs((date4-dataq)/365.25) THEN date_start1=date4;
FORMAT date_start1 DATE9.;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I don't get good results. SAS doesn't pick this:&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;date_start1=date1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;It seems SAS only picks:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;IF abs((date1-dataq)/365.25) GT abs((date2-dataq)/365.25) THEN date_start1=date2;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What should I do?&lt;/P&gt;
&lt;P&gt;Thanks in advance!&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;There is NO way for SAS to have a value of date_start1= date1. (unless date1 matches one of date2, 3 or 4)&lt;/P&gt;
&lt;P&gt;Your if/then/else forces it to have one of the values of date2, date3 or date4.&lt;/P&gt;
&lt;P&gt;Perhaps you meant to have another else that left date_start1 with a value of date1 or a different test at the start before the assignment of date2?&lt;/P&gt;</description>
      <pubDate>Mon, 13 Nov 2017 18:10:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Problems-with-If-then-else-statement/m-p/412994#M101034</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-11-13T18:10:54Z</dc:date>
    </item>
  </channel>
</rss>

