<?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: Multiple IF, THEN, DO, ELSE not producing consistent results in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414878#M280087</link>
    <description>&lt;P&gt;The fact that you do not format your code in a readable way, and use upcase coding really makes your code hard to read.&amp;nbsp; From what I gather from your post, I would imagine your issue is caused by using the lag() functions in if's.&amp;nbsp; You can read several posts on here about why it is not a good idea to do this.&amp;nbsp; For my money I would find it far simpler to transpose (yes, I know against what I normally suggest) and then do the conditionals:&lt;/P&gt;
&lt;PRE&gt;data have;
  infile datalines dlm = ':' truncover;
  length source $10. id1 $2. id2 $2. phone $15.;
  input source $ id1 $ id2 $ phone $ ;
datalines;
SOURCE1: 1: 1: 5555555:
SOURCE2: 1: 1: 5555555:
DIFF: 1: 1: .:
SOURCE1: 1: 2: :
SOURCE2: 1: 2: :
DIFF: 1: 2: .:
SOURCE1: 2: 1: 9999999:
SOURCE2: 2: 1: 
DIFF: 2: 1: XXXXXXX:
SOURCE1: 3: 1:  :
SOURCE2: 3: 1: 4444444:
DIFF: 3: 1: XXXXXXX:
SOURCE1: 4: 1: 4448888:
SOURCE2: 4: 1: 4444444:
DIFF: 4: 1: ...XXXX:
;
run;

proc transpose data=have out=want;
  by id1 id2;
  var phone;
  id source;
run;&lt;/PRE&gt;
&lt;P&gt;This should put all the necessary information on one row.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Nov 2017 16:07:18 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2017-11-20T16:07:18Z</dc:date>
    <item>
      <title>Multiple IF, THEN, DO, ELSE not producing consistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414871#M280085</link>
      <description>&lt;P&gt;Good morning,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; I have spent days trying to work the logic on some IF, THEN, DO, ELSE statements and every time I get one to work, the other doesn't.&lt;/P&gt;&lt;P&gt;I am using SAS 9.2. I have tried consecutive IF, THEN, DO statements to try to make the IF THEN ELSE statements read certain blocks of data but that hasn't been working for me. I have tried everything I know how to try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Background:&lt;/P&gt;&lt;P&gt;The data is a result of comparing two datasets for differences using PROC COMPARE.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need:&lt;/P&gt;&lt;P&gt;I need to be able to differentiate between&amp;nbsp;a difference as a result of having a value missing in one dataset and not the other&amp;nbsp;and when the difference is as a&amp;nbsp;result of an actual difference (no missing values).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Issues:&lt;/P&gt;&lt;P&gt;I found I needed to remove the X's from difference because sometimes the if statements would work if the DIFF value was blank. I included two WANT2 tables to show a couple of examples of what I've tried. But I could have included 50. I can get everything to = 1 or MISSING, but not either.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile datalines dlm = ':' truncover;
length SOURCE $10. ID1 $2. ID2 $2. PHONE $15.;
input SOURCE $ ID1 $ ID2 $ PHONE $ ;
datalines;
SOURCE1: 1: 1: 5555555:
SOURCE2: 1: 1: 5555555:
DIFF: 1: 1: .:
SOURCE1: 1: 2: :
SOURCE2: 1: 2: :
DIFF: 1: 2: .:
SOURCE1: 2: 1: 9999999:
SOURCE2: 2: 1: 
DIFF: 2: 1: XXXXXXX:
SOURCE1: 3: 1:  :
SOURCE2: 3: 1: 4444444:
DIFF: 3: 1: XXXXXXX:
SOURCE1: 4: 1: 4448888:
SOURCE2: 4: 1: 4444444:
DIFF: 4: 1: ...XXXX:
;
run;

 
data want;
set have;
if SOURCE = "DIFF" and index(PHONE,'X') then PHONE = "";
else PHONE = PHONE;
run;

data want2;
set have;
by 
ID1 ID2;
if SOURCE = "SOURCE2" then do;
if PHONE = "" and lag(PHONE) ne "" then do;
if SOURCE = "DIFF" then PHONE = "MISSING";
end;

else if PHONE ne "" and lag(PHONE) = "" then do;
if SOURCE = "DIFF" then PHONE = "MISSING";
end;

else if PHONE ne lag(PHONE) then do;
if SOURCE = "DIFF" then PHONE = '1';
end;

end;

run;

 

 

data want2;
set have;
by 
ID1 ID2;
if SOURCE = "SOURCE2" and PHONE = "" and lag(PHONE) ne "" then do;
if SOURCE = "DIFF" then PHONE = "MISSING";
end;

else if SOURCE = "SOURCE2" and PHONE ne "" and lag(PHONE) = "" then do;
if SOURCE = "DIFF" then PHONE = "MISSING";
end;

else if SOURCE = "SOURCE2" and PHONE ne lag(PHONE) then do;
if SOURCE = "DIFF" then PHONE = '1';
end;

run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;I would love the end result to be this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SOURCE&amp;nbsp;&amp;nbsp;&amp;nbsp; ID1&amp;nbsp; ID2&amp;nbsp;&amp;nbsp; PHONE&lt;/P&gt;&lt;P&gt;SOURCE1&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;5555555&lt;/P&gt;&lt;P&gt;SOURCE2&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5555555&lt;/P&gt;&lt;P&gt;DIFF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SOURCE1&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;SOURCE2&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;DIFF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/P&gt;&lt;P&gt;SOURCE1&amp;nbsp; &amp;nbsp;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 9999999&lt;/P&gt;&lt;P&gt;SOURCE2&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DIFF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MISSING&lt;/P&gt;&lt;P&gt;SOURCE1&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;1&lt;/P&gt;&lt;P&gt;SOURCE2&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4444444&lt;/P&gt;&lt;P&gt;DIFF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MISSING&lt;/P&gt;&lt;P&gt;SOURCE1&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4448888&lt;/P&gt;&lt;P&gt;SOURCE2&amp;nbsp;&amp;nbsp;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4447777&lt;/P&gt;&lt;P&gt;DIFF&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where am I going wrong?&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2017 15:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414871#M280085</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2017-11-20T15:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple IF, THEN, DO, ELSE not producing consistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414876#M280086</link>
      <description>&lt;P&gt;While I haven't gone through this in a lot of detail, the LAG function inside an IF statement doesn't work the way you think it should work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to use the LAG function in a command that always executes and is not inside an IF statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, in dataset WANT2, try this immediately after the BY command:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;prevphone=lag(phone);&lt;/PRE&gt;
&lt;P&gt;and of course use prevphone in the rest of the code.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2017 16:06:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414876#M280086</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-11-20T16:06:05Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple IF, THEN, DO, ELSE not producing consistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414878#M280087</link>
      <description>&lt;P&gt;The fact that you do not format your code in a readable way, and use upcase coding really makes your code hard to read.&amp;nbsp; From what I gather from your post, I would imagine your issue is caused by using the lag() functions in if's.&amp;nbsp; You can read several posts on here about why it is not a good idea to do this.&amp;nbsp; For my money I would find it far simpler to transpose (yes, I know against what I normally suggest) and then do the conditionals:&lt;/P&gt;
&lt;PRE&gt;data have;
  infile datalines dlm = ':' truncover;
  length source $10. id1 $2. id2 $2. phone $15.;
  input source $ id1 $ id2 $ phone $ ;
datalines;
SOURCE1: 1: 1: 5555555:
SOURCE2: 1: 1: 5555555:
DIFF: 1: 1: .:
SOURCE1: 1: 2: :
SOURCE2: 1: 2: :
DIFF: 1: 2: .:
SOURCE1: 2: 1: 9999999:
SOURCE2: 2: 1: 
DIFF: 2: 1: XXXXXXX:
SOURCE1: 3: 1:  :
SOURCE2: 3: 1: 4444444:
DIFF: 3: 1: XXXXXXX:
SOURCE1: 4: 1: 4448888:
SOURCE2: 4: 1: 4444444:
DIFF: 4: 1: ...XXXX:
;
run;

proc transpose data=have out=want;
  by id1 id2;
  var phone;
  id source;
run;&lt;/PRE&gt;
&lt;P&gt;This should put all the necessary information on one row.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2017 16:07:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414878#M280087</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2017-11-20T16:07:18Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple IF, THEN, DO, ELSE not producing consistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414883#M280088</link>
      <description>&lt;P&gt;Thank you for your response RW9. I will certainly not be using lag in IF statements any more. I like your idea of using transpose, but my actual data set is way too large for transpose to work. Thanks again (and I'll work on making my code more readable).&lt;/P&gt;</description>
      <pubDate>Mon, 20 Nov 2017 16:24:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414883#M280088</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2017-11-20T16:24:26Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple IF, THEN, DO, ELSE not producing consistent results</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414887#M280089</link>
      <description>&lt;P&gt;Thank you PaigeMiller. I have learned my lesson with lag. Your suggestion worked great. I had to add some coding to get my desired results, but everything looks just the way it should. Thank you so much. I cannot wait to put this issue to rest!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want3 (drop = phone_result_fill prevphone phone_result);
set want;
by 
ID1 ID2;

prevphone = lag(phone);

if source = "source2" then do;
if phone = "" and prevphone ne "" or phone ne "" and prevphone = "" then phone_result = "MISSING";
else if phone ne prevphone then phone_result = "1";
end;

retain phone_result_fill;
if not missing (phone_result) then phone_result_fill = phone_result;
phone_result = phone_result_fill;

if source = "DIFF" then phone = phone_result;

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Nov 2017 16:29:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Multiple-IF-THEN-DO-ELSE-not-producing-consistent-results/m-p/414887#M280089</guid>
      <dc:creator>sas-inquirer</dc:creator>
      <dc:date>2017-11-20T16:29:01Z</dc:date>
    </item>
  </channel>
</rss>

