<?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 How do I use the Lag Function based on Key Variables-ID, Date and Training Type in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631315#M187036</link>
    <description>&lt;P&gt;I have the following data set:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lag_Data_Have_031120.PNG" style="width: 310px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36770i7CCFC066804A3BB1/image-size/large?v=v2&amp;amp;px=999" role="button" title="Lag_Data_Have_031120.PNG" alt="Lag_Data_Have_031120.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to figure out how to capture the previous (most recent) training of an employee (represented by ID) given that they took the IT training. My goal is to have the following table:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lag_Data_Want_031120.PNG" style="width: 399px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36773iE8AD38854201B0FE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Lag_Data_Want_031120.PNG" alt="Lag_Data_Want_031120.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm assuming that I can use the lag function to accomplish this but I'm having a hard time trying to figure out how to set up my SAS code (using SAS studio).&lt;/P&gt;</description>
    <pubDate>Wed, 11 Mar 2020 17:49:05 GMT</pubDate>
    <dc:creator>kingsii24</dc:creator>
    <dc:date>2020-03-11T17:49:05Z</dc:date>
    <item>
      <title>How do I use the Lag Function based on Key Variables-ID, Date and Training Type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631315#M187036</link>
      <description>&lt;P&gt;I have the following data set:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lag_Data_Have_031120.PNG" style="width: 310px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36770i7CCFC066804A3BB1/image-size/large?v=v2&amp;amp;px=999" role="button" title="Lag_Data_Have_031120.PNG" alt="Lag_Data_Have_031120.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to figure out how to capture the previous (most recent) training of an employee (represented by ID) given that they took the IT training. My goal is to have the following table:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Lag_Data_Want_031120.PNG" style="width: 399px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/36773iE8AD38854201B0FE/image-size/large?v=v2&amp;amp;px=999" role="button" title="Lag_Data_Want_031120.PNG" alt="Lag_Data_Want_031120.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm assuming that I can use the lag function to accomplish this but I'm having a hard time trying to figure out how to set up my SAS code (using SAS studio).&lt;/P&gt;</description>
      <pubDate>Wed, 11 Mar 2020 17:49:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631315#M187036</guid>
      <dc:creator>kingsii24</dc:creator>
      <dc:date>2020-03-11T17:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use the Lag Function based on Key Variables-ID, Date and Training Type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631320#M187041</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299307"&gt;@kingsii24&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Welcome to the community!&lt;/P&gt;
&lt;P&gt;You can try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=have_sorted;
	by ID Date_start;
run;

data want;
	set have_sorted;
	by ID Date_start;
	length prev_training $ 10;
	_lag = lag(training_type);
	if first.ID then _lag="";
	if training_type = "IT" then prev_training = _lag;
	drop _lag;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;NB: there are some inconsistencies in the pictures for patient 2 -&amp;gt; the start dates for each training are not the same. I believe it is a mistake.&lt;/P&gt;
&lt;P&gt;If not, please explain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;</description>
      <pubDate>Wed, 11 Mar 2020 18:11:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631320#M187041</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-03-11T18:11:21Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use the Lag Function based on Key Variables-ID, Date and Training Type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631344#M187054</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/292097"&gt;@ed_sas_member&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Thank you so much! This is what I needed, the discrepancy between the two data sets is definitely an error on my side.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Mar 2020 18:49:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631344#M187054</guid>
      <dc:creator>kingsii24</dc:creator>
      <dc:date>2020-03-11T18:49:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use the Lag Function based on Key Variables-ID, Date and Training Type</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631353#M187063</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/299307"&gt;@kingsii24&lt;/a&gt;!&lt;/P&gt;</description>
      <pubDate>Wed, 11 Mar 2020 19:23:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-Lag-Function-based-on-Key-Variables-ID-Date-and/m-p/631353#M187063</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-03-11T19:23:55Z</dc:date>
    </item>
  </channel>
</rss>

