<?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: Lag function with if statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514840#M138864</link>
    <description>&lt;P&gt;You can use lag in conditional statements lag but indeed it is tricky, either you populate the lagged var in another variable or use ifc/ifn&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your logic is correct and requires a&lt;STRONG&gt; very minor&lt;/STRONG&gt; tweak as i mentioned, populate lagged var in another variable as illustrated below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
INFILE DATALINES DLM='	';
LENGTH NAME $ 20;
INPUT NAME $ ID;
DATALINES;
ALEX	123456
ALEX	123456
ALEXANDER	123456
;
RUN;


DATA WANT;
 SET HAVE;
 temp=lag(name);
 IF ID = LAG(ID)
     AND NAME NE LAG(NAME)
 THEN NAME =temp;
 drop temp;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
DATA HAVE;
INFILE DATALINES DLM='	';
LENGTH NAME $ 20;
INPUT NAME $ ID;
DATALINES;
ALEX	123456
ALEX	123456
ALEXANDER	123456
;
RUN;

DATA WANT;
 SET HAVE;
 name=ifc( ID = LAG(ID) and  NAME NE LAG(NAME),lag(name),name);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 20 Nov 2018 16:57:37 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2018-11-20T16:57:37Z</dc:date>
    <item>
      <title>Lag function with if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514818#M138852</link>
      <description>&lt;P&gt;Hello guys,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a table containning different names for the same registry. The error is due to the name variable being pulled from&amp;nbsp;different databases and is simply a different spelling since the ID is definitely the same.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to correct this error by simply substituting one of the versions of the name by the other.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I came up with the following table to exemplify my problem:&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='	';
LENGTH NAME $ 20;
INPUT NAME $ ID;
DATALINES;
ALEX	123456
ALEX	123456
ALEXANDER	123456
;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;To try and correct it I wanted to use the lag function to check if the ID from the current row is the same from the row above but with a different name and, when that is the case, substitute the name in the current row with the name on the previous&amp;nbsp;row. I came up with the following code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA WANT;
 SET HAVE;
 IF ID = LAG(ID)
     AND NAME NE LAG(NAME)
 THEN NAME = LAG(NAME);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But this code gives me a blank when the result I was expecting would be Alex. I was able to determine that the problem is in the IF statement, because using only the the lag(name) as a new variable, without the if statement, it works fine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am at a loss of what the problem might be and would appreciate any help or pointers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks in advance for the help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alex (Alexander : ) )&lt;/P&gt;</description>
      <pubDate>Tue, 20 Nov 2018 16:23:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514818#M138852</guid>
      <dc:creator>alexdamado</dc:creator>
      <dc:date>2018-11-20T16:23:41Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514821#M138855</link>
      <description>&lt;P&gt;Don't use conditional assignments with lag.&amp;nbsp; See introduction in:&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings09/055-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/055-2009.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The easiest way to fix your data is to first extract the id and decode you want, be it the first or some other calculation, then merge that back onto the original data.&amp;nbsp; Alternatively you could retain a value, e.g:&lt;/P&gt;
&lt;PRE&gt;data want;
  set have;
  by id;
  length lstname $200;
  retain lstname;
  if first.id then lstname=name;
  else if name ne lstname then name=lstname;
run;&lt;/PRE&gt;
&lt;P&gt;Note how I don't code all in uppercase as I don't want to shout the code at you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Nov 2018 16:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514821#M138855</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-11-20T16:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514825#M138858</link>
      <description>&lt;P&gt;Thank you for the quick reply and apologies for the shouting : )&lt;/P&gt;</description>
      <pubDate>Tue, 20 Nov 2018 16:34:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514825#M138858</guid>
      <dc:creator>alexdamado</dc:creator>
      <dc:date>2018-11-20T16:34:58Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514830#M138860</link>
      <description>&lt;P&gt;Your two different calls to LAG(NAME) are maintaining two different stacks of values.&amp;nbsp; Since the second one only executes occasionally the values it pulls back off the stack are totally different.&lt;/P&gt;
&lt;P&gt;It looks like you are trying to keep the first copy of the name.&amp;nbsp; So just tell SAS that is what you are doing.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  by id;
  if first.id then name1 = name;
  name=name1;
  retain name1 ;
  drop name1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Nov 2018 16:44:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514830#M138860</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-11-20T16:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: Lag function with if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514840#M138864</link>
      <description>&lt;P&gt;You can use lag in conditional statements lag but indeed it is tricky, either you populate the lagged var in another variable or use ifc/ifn&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So your logic is correct and requires a&lt;STRONG&gt; very minor&lt;/STRONG&gt; tweak as i mentioned, populate lagged var in another variable as illustrated below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
INFILE DATALINES DLM='	';
LENGTH NAME $ 20;
INPUT NAME $ ID;
DATALINES;
ALEX	123456
ALEX	123456
ALEXANDER	123456
;
RUN;


DATA WANT;
 SET HAVE;
 temp=lag(name);
 IF ID = LAG(ID)
     AND NAME NE LAG(NAME)
 THEN NAME =temp;
 drop temp;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;or&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
DATA HAVE;
INFILE DATALINES DLM='	';
LENGTH NAME $ 20;
INPUT NAME $ ID;
DATALINES;
ALEX	123456
ALEX	123456
ALEXANDER	123456
;
RUN;

DATA WANT;
 SET HAVE;
 name=ifc( ID = LAG(ID) and  NAME NE LAG(NAME),lag(name),name);
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 20 Nov 2018 16:57:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Lag-function-with-if-statement/m-p/514840#M138864</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-11-20T16:57:37Z</dc:date>
    </item>
  </channel>
</rss>

