<?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: How do I use the lag function based on key variables to sum over a period of time in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-lag-function-based-on-key-variables-to-sum-over/m-p/655072#M196616</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;Here is an attempt to achieve this.&lt;/P&gt;
&lt;P&gt;I just wondered why sum_rev_training is equal to 3 and not 4 for ID=3 ? Is it a mistake?&lt;/P&gt;
&lt;P&gt;Best,&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=" " dsd missover;
	input ID Training_T $ Date_start:MMDDYY10. Date_end:MMDDYY10. Month_fr Prev_train $;
	format Date_start Date_end MMDDYY10.;
	datalines;
1 IT 1/1/2019 1/31/2019 1
1 BT 2/1/2019 2/8/2019 0.285714
1 IT 2/9/2019 2/28/2019 0.714286 BT
2 BT 11/1/2018 11/11/2018 0.366667
2 IT 11/12/2018 11/30/2018 0.633333 BT
2 IIT 12/1/2018 12/31/2018 1
2 BT 1/1/2019 1/31/2019 1
2 IIT 2/1/2019 2/28/2019 1
3 IT 10/1/2018 10/31/2018 1
3 IT 11/1/2018 11/30/2018 1 IT
3 IT 1/1/2019 1/31/2019 1 IT
3 IT 2/1/2019 2/28/2019 1 IT
3 IT 12/1/2019 12/31/2019 1 IT
4 IIT 9/1/2018 9/30/2018 1
4 BT 10/1/2018 10/14/2018 0.451613
4 IIT 10/15/2018 10/31/2018 0.548387
4 IIT 11/1/2018 11/2/2018 0.066667
4 BT 11/3/2018 11/30/2018 0.933333
;
run;

data want;
	set have;
	by ID Training_T notsorted;
	_lag = lag(Month_fr);
	if not missing(Prev_train) then _Sum_rev_training + _lag;
	else call missing(_Sum_rev_training);
	if last.Training_T then Sum_rev_training = _Sum_rev_training;
	drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 09 Jun 2020 07:29:58 GMT</pubDate>
    <dc:creator>ed_sas_member</dc:creator>
    <dc:date>2020-06-09T07:29:58Z</dc:date>
    <item>
      <title>How do I use the lag function based on key variables to sum over a period of time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-lag-function-based-on-key-variables-to-sum-over/m-p/654712#M196603</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kingsii24_0-1591662910195.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/40930i006F265E4C8997F6/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kingsii24_0-1591662910195.png" alt="kingsii24_0-1591662910195.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;In the above data set I've identified the previous (most recent) training an individual (represented by ID) prior to taking an IT training, see column labeled "Prev_Train". Now, I'm trying to figure out how to count the length&amp;nbsp;of time an individual was enrolled in their previous training for, prior to enrolling in their IT training.&amp;nbsp;For example, for ID 1, prior to enrolling in IT training, they were enrolled in BT training for 0.285714 months. See below for what the final table should look like:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kingsii24_1-1591662933443.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/40931i97F5EA2791A20A97/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kingsii24_1-1591662933443.png" alt="kingsii24_1-1591662933443.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 00:46:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-lag-function-based-on-key-variables-to-sum-over/m-p/654712#M196603</guid>
      <dc:creator>kingsii24</dc:creator>
      <dc:date>2020-06-09T00:46:56Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use the lag function based on key variables to sum over a period of time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-lag-function-based-on-key-variables-to-sum-over/m-p/655072#M196616</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;Here is an attempt to achieve this.&lt;/P&gt;
&lt;P&gt;I just wondered why sum_rev_training is equal to 3 and not 4 for ID=3 ? Is it a mistake?&lt;/P&gt;
&lt;P&gt;Best,&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=" " dsd missover;
	input ID Training_T $ Date_start:MMDDYY10. Date_end:MMDDYY10. Month_fr Prev_train $;
	format Date_start Date_end MMDDYY10.;
	datalines;
1 IT 1/1/2019 1/31/2019 1
1 BT 2/1/2019 2/8/2019 0.285714
1 IT 2/9/2019 2/28/2019 0.714286 BT
2 BT 11/1/2018 11/11/2018 0.366667
2 IT 11/12/2018 11/30/2018 0.633333 BT
2 IIT 12/1/2018 12/31/2018 1
2 BT 1/1/2019 1/31/2019 1
2 IIT 2/1/2019 2/28/2019 1
3 IT 10/1/2018 10/31/2018 1
3 IT 11/1/2018 11/30/2018 1 IT
3 IT 1/1/2019 1/31/2019 1 IT
3 IT 2/1/2019 2/28/2019 1 IT
3 IT 12/1/2019 12/31/2019 1 IT
4 IIT 9/1/2018 9/30/2018 1
4 BT 10/1/2018 10/14/2018 0.451613
4 IIT 10/15/2018 10/31/2018 0.548387
4 IIT 11/1/2018 11/2/2018 0.066667
4 BT 11/3/2018 11/30/2018 0.933333
;
run;

data want;
	set have;
	by ID Training_T notsorted;
	_lag = lag(Month_fr);
	if not missing(Prev_train) then _Sum_rev_training + _lag;
	else call missing(_Sum_rev_training);
	if last.Training_T then Sum_rev_training = _Sum_rev_training;
	drop _:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Jun 2020 07:29:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-lag-function-based-on-key-variables-to-sum-over/m-p/655072#M196616</guid>
      <dc:creator>ed_sas_member</dc:creator>
      <dc:date>2020-06-09T07:29:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use the lag function based on key variables to sum over a period of time</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-lag-function-based-on-key-variables-to-sum-over/m-p/655225#M196621</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Not sure if I'm understanding you logic well, but I think that using BY-groups for your ID variable , and adding some if-the-else statements could get you pretty far.&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 cards;
input ID Training_T : $3. Date_start : mmddyy10. Date_End : mmddyy10. ;
last_day= intnx('month',Date_Start,0,'end');
Months_F=(intck('day', Date_start, Date_End)+1) / day(last_day);

format Date_start date9. 
	   Date_End date9. 
	   last_day date9.;
drop last_day ;	   
cards;
1 IT   1/1/2019   1/31/2019  
1 BT   2/1/2019   2/08/2019 
1 IT   2/9/2019   2/28/2019
2 BT  11/1/2018  11/11/2018
2 IT  11/12/2018 11/30/2018
2 IIT 12/1/2018  12/31/2018
2 BT   1/1/2019   1/31/2019
2 IIT  2/1/2019   2/28/2019
3 IT  10/1/2018  10/31/2018
3 IT  11/1/2018  11/30/2018
3 IT   1/1/2019   1/31/2019
3 IT   2/1/2019   2/28/2019
3 IT  12/1/2019  12/31/2019
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and here is a data step with by groups:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have;
 by ID;
 if first.id then sum_temp=0;
 Prev_Training=LAG(Training_T);
 if training_t ne 'IT' then Prev_Training='';
 if Training_T ne 'IT' then sum_temp + Months_F;
 if Training_t = 'IT' then Sum_Prev_Training = sum_temp;
 drop sum_temp ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and this is the result:&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-inline" image-alt="2020-06-09_100428.png" style="width: 609px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/41387i3B1D9A9897ABF421/image-size/large?v=v2&amp;amp;px=999" role="button" title="2020-06-09_100428.png" alt="2020-06-09_100428.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;as the ID "3" has only taken IT training, it is not clear how you want to process that data ?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Greetings,&lt;/P&gt;
&lt;P&gt;Mathias.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jun 2020 08:05:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-the-lag-function-based-on-key-variables-to-sum-over/m-p/655225#M196621</guid>
      <dc:creator>MCoopmans</dc:creator>
      <dc:date>2020-06-09T08:05:54Z</dc:date>
    </item>
  </channel>
</rss>

