<?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: SAS Lag in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567565#M11535</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274150"&gt;@lisa2002&lt;/a&gt;&amp;nbsp;- it looks like you've edited your post removing the code fragment since I replied mentioning your use of the lag function. In any case this is what I meant by never using lag inside a conditional statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue with lag is that, contrary to what you might think, it doesn't return the value from the previous observation. Instead it returns the value from the last time the function was executed. Here's a simple example using the SASHELP.CLASS data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Assume we want to populate the variable called "Previous" */
/* with the value from the previous observation but ONLY */
/* if the current observation has the value "M" for the sex */
/* variable */


/* Incorrect way of using lag */

data lagged1;
	length previous $8.;
	set sashelp.class;
	if sex="M" then previous=lag(name);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is what the output looks like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Incorrect use of lag.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30422iA036140DB6EA4B5C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Incorrect use of lag.png" alt="Incorrect use of lag.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll notice that for observation number 5 you'd expect the value of Previous to be "Carol" but actually it's "Alfred" - the last time the if condition was true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the correct way to do it&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lagged2(drop=temp);
	length previous $8.;
	set sashelp.class;
	temp=lag(name);
	if sex="M" then previous=temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output is now&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Correct way to use lag.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30423i40E267F109808485/image-size/large?v=v2&amp;amp;px=999" role="button" title="Correct way to use lag.png" alt="Correct way to use lag.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now it works as desired because the lag function is executed outside the if condition and so always executes.&lt;/P&gt;</description>
    <pubDate>Thu, 20 Jun 2019 09:44:52 GMT</pubDate>
    <dc:creator>ChrisBrooks</dc:creator>
    <dc:date>2019-06-20T09:44:52Z</dc:date>
    <item>
      <title>SAS Lag</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567081#M11438</link>
      <description>&lt;P&gt;SAS members I have a question and I hope someone can assist as soon as possible. I have a data set where I need to match up the duplicates while combing number, rx_number, start_date, end_date, sig, and rx_name in one column but separate each rx1 rx2 and so on while keeping the information for the patient together in one row... basically I need it to look like this&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RX1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RX2&lt;/P&gt;&lt;P&gt;NS4747 - 123456 - tylenol - 5/7/2019 - 9/3/2019 -&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; NS4747 - 123456 - dayquil - 5/7/2019 - 9/3/2019 -&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TAKE 1 TABLET(S) ORALLY AT BEDTIME -&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;TAKE 1 TABLET(S) ORALLY AT BEDTIME -&lt;/P&gt;&lt;P&gt;M.E - [PSYCHIATRY]&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;M.E - [PSYCHIATRY]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is sample data&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patients&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;Number&amp;nbsp; &amp;nbsp;RX_NUMBER&amp;nbsp; START_DATE&amp;nbsp; &amp;nbsp; END_DATE&amp;nbsp; &amp;nbsp;SIG&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RX_NAME&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;John Doe&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NS4747&amp;nbsp; &amp;nbsp;123456&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/16/19&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6/18/19&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;TAKE 1 TABLET(S) ORALLY&amp;nbsp; &amp;nbsp; &amp;nbsp;Tylenol&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AT BEDTIME&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Patients&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;Number&amp;nbsp; &amp;nbsp;RX_NUMBER&amp;nbsp; START_DATE&amp;nbsp; &amp;nbsp; END_DATE&amp;nbsp; &amp;nbsp;SIG&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RX_NAME&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;John Doe&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NS4747&amp;nbsp; &amp;nbsp;123456&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/16/19&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6/18/19&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;TAKE 1 TABLET(S) ORALLY&amp;nbsp; &amp;nbsp; &amp;nbsp;dayquil&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AT BEDTIME&lt;/P&gt;&lt;P&gt;Patients&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;Number&amp;nbsp; &amp;nbsp;RX_NUMBER&amp;nbsp; START_DATE&amp;nbsp; &amp;nbsp; END_DATE&amp;nbsp; &amp;nbsp;SIG&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RX_NAME&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jane Doe&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;NS1234&amp;nbsp; &amp;nbsp;78912&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;6/14/19&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6/17/19&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;TAKE 1 TABLET(S) ORALLY&amp;nbsp; &amp;nbsp; &amp;nbsp;claritin&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;AT BEDTIME&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My code is causing the issue where the rx_number from John Doe is caring over to Jane Doe and so on... Please tell me what I am doing wrong.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank You. FEELING OVERWHELMED&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 16:41:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567081#M11438</guid>
      <dc:creator>lisa2002</dc:creator>
      <dc:date>2019-06-19T16:41:42Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Lag</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567085#M11442</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274150"&gt;@lisa2002&lt;/a&gt;&amp;nbsp;the first thing that strikes me is that you should never use the lag function inside a conditional statement. You have several instances of using lag() inside an if condition as you can get incorrect results. Your best plan is to do that lags first, store the values in variables, then use the if statement on those variables.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jun 2019 22:32:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567085#M11442</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2019-06-18T22:32:10Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Lag</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567352#M11485</link>
      <description>ChrisBrooks&lt;BR /&gt;Can you help guide me in the direction I should go to receive the results I'm looking for?</description>
      <pubDate>Wed, 19 Jun 2019 16:44:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567352#M11485</guid>
      <dc:creator>lisa2002</dc:creator>
      <dc:date>2019-06-19T16:44:33Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Lag</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567356#M11489</link>
      <description>&lt;P&gt;Why do you think you need to use LAG at all?&lt;/P&gt;
&lt;P&gt;If&amp;nbsp; you are just transposes from multiple observations per patient to one observation per patient then there is no need for LAG().&amp;nbsp; So if patient X has 4 observations the resulting table will have RX1 to RX4 populated for this patient.&lt;/P&gt;
&lt;P&gt;Personally I would just use one step to generate the concatenated string and use PROC TRANPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data step1;
  set have;
  length rx $2000 ;
  rx=catx(' - ',number,rx_number,.....);
run;
proc transpose data=step1 out=want prefix=rx;
  by patients;
  var rx;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or are you trying to collapse multiple records for the same drug into one?&amp;nbsp; If so what is the criteria to know how to collapse the records?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 16:55:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567356#M11489</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-06-19T16:55:01Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Lag</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567399#M11499</link>
      <description>&lt;P&gt;multiple records for the same PATIENT into one. So basically some patients have one prescription and some have 2 or more I need the the prescriptions to match the Patient on one row&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jun 2019 18:33:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567399#M11499</guid>
      <dc:creator>lisa2002</dc:creator>
      <dc:date>2019-06-19T18:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Lag</title>
      <link>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567565#M11535</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/274150"&gt;@lisa2002&lt;/a&gt;&amp;nbsp;- it looks like you've edited your post removing the code fragment since I replied mentioning your use of the lag function. In any case this is what I meant by never using lag inside a conditional statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The issue with lag is that, contrary to what you might think, it doesn't return the value from the previous observation. Instead it returns the value from the last time the function was executed. Here's a simple example using the SASHELP.CLASS data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Assume we want to populate the variable called "Previous" */
/* with the value from the previous observation but ONLY */
/* if the current observation has the value "M" for the sex */
/* variable */


/* Incorrect way of using lag */

data lagged1;
	length previous $8.;
	set sashelp.class;
	if sex="M" then previous=lag(name);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This is what the output looks like&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Incorrect use of lag.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30422iA036140DB6EA4B5C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Incorrect use of lag.png" alt="Incorrect use of lag.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You'll notice that for observation number 5 you'd expect the value of Previous to be "Carol" but actually it's "Alfred" - the last time the if condition was true.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the correct way to do it&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data lagged2(drop=temp);
	length previous $8.;
	set sashelp.class;
	temp=lag(name);
	if sex="M" then previous=temp;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output is now&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Correct way to use lag.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/30423i40E267F109808485/image-size/large?v=v2&amp;amp;px=999" role="button" title="Correct way to use lag.png" alt="Correct way to use lag.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now it works as desired because the lag function is executed outside the if condition and so always executes.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jun 2019 09:44:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/SAS-Lag/m-p/567565#M11535</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2019-06-20T09:44:52Z</dc:date>
    </item>
  </channel>
</rss>

