<?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: Adding start and stop variables based on number of days in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455908#M14069</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $	total_days;
datalines;
A	60
A	90
A	60
B	40
B	20
B	90
B	70
;
data want;
 set have;
 by id;
 if first.id then stop=0;
 stop+total_days;
 start=lag(stop)+1;
 if first.id then start=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 20 Apr 2018 13:13:51 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2018-04-20T13:13:51Z</dc:date>
    <item>
      <title>Adding start and stop variables based on number of days</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455763#M14063</link>
      <description>&lt;P&gt;I have a dataset in the form of&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;total_days&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;TD&gt;70&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to add two variables whose values would represent the start and stop days of each row. For instance, for id A, it would be&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;id&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;total_days&lt;/TD&gt;&lt;TD&gt;start&lt;/TD&gt;&lt;TD&gt;stop&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;TD&gt;61&lt;/TD&gt;&lt;TD&gt;150&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;60&lt;/TD&gt;&lt;TD&gt;151&lt;/TD&gt;&lt;TD&gt;210&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used the following code and was fairly confident that it would give me what I was looking for. However, the start and stop variables were populated only in the first row of each id and were missing in other rows. I am unable to understand why the codes did not work.&amp;nbsp;&lt;/P&gt;&lt;P&gt;data cum_days;&lt;BR /&gt;set num_days;&lt;BR /&gt;by id;&lt;BR /&gt;if first.id then do;&lt;BR /&gt;start=1;&lt;BR /&gt;stop=total_days;&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;start=lag(stop) + 1;&lt;BR /&gt;stop= start + total_days -1;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Apr 2018 21:29:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455763#M14063</guid>
      <dc:creator>Mahip</dc:creator>
      <dc:date>2018-04-19T21:29:19Z</dc:date>
    </item>
    <item>
      <title>Re: Adding start and stop variables based on number of days</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455764#M14064</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $	total_days;
datalines;
A	60
A	90
A	60
B	40
B	20
B	90
B	70
;

data want;
set have;
by id;
retain start stop;
if first.id then do; start=1;stop=total_days;end;
else do;start=stop+1;stop+total_days;end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Apr 2018 21:35:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455764#M14064</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-04-19T21:35:14Z</dc:date>
    </item>
    <item>
      <title>Re: Adding start and stop variables based on number of days</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455765#M14065</link>
      <description>&lt;P&gt;The issue is the LAG function queue behavior.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You might consider RETAINing the value of Stop&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id;
   Retain stop;
   if first.id then do;
      start=1;
      stop=total_days;
   end;
   else do;
      start=stop + 1;
      stop= start + total_days -1;
   end;
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Apr 2018 21:38:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455765#M14065</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-04-19T21:38:27Z</dc:date>
    </item>
    <item>
      <title>Re: Adding start and stop variables based on number of days</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455768#M14066</link>
      <description>&lt;P&gt;No lag, no retain required with a DO UNTIL() loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $	total_days;
datalines;
A	60
A	90
A	60
B	40
B	20
B	90
B	70
;

data want;
stop = 0;
do until (last.id);
    set have; by id;
    start = stop + 1;
    stop + total_days;
    output;
    end;
run;

proc print data=want noobs; var id total_days start stop; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Apr 2018 22:08:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455768#M14066</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2018-04-19T22:08:13Z</dc:date>
    </item>
    <item>
      <title>Re: Adding start and stop variables based on number of days</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455908#M14069</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id $	total_days;
datalines;
A	60
A	90
A	60
B	40
B	20
B	90
B	70
;
data want;
 set have;
 by id;
 if first.id then stop=0;
 stop+total_days;
 start=lag(stop)+1;
 if first.id then start=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Apr 2018 13:13:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/455908#M14069</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-20T13:13:51Z</dc:date>
    </item>
    <item>
      <title>Re: Adding start and stop variables based on number of days</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/456102#M14080</link>
      <description>&lt;P&gt;Thank you very much! I would also like to understand how your set of codes were processed differently than mine. It would be of much help if you could walk me through your codes, so that I would be able to solve similar issues on my own in future. Also, what was the issue with my codes?&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 21:51:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/456102#M14080</guid>
      <dc:creator>Mahip</dc:creator>
      <dc:date>2018-04-20T21:51:10Z</dc:date>
    </item>
    <item>
      <title>Re: Adding start and stop variables based on number of days</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/456156#M14084</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;/*******stop is cumulative sum of total_days, I think you understand it.***********/&lt;BR /&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; stop&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;0&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;; /*since cum is happened in a group,so set it be zero at the first obs.*/&lt;/SPAN&gt;
 stop&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;total_days&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;/*it is easy to understand. get lag value of stop and + 1 as start variable*/
 start&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;lag&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;stop&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;+&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;/*when reaching the first obs of a group, start would be the last stop of the previous group*/&lt;BR /&gt;/*Therefore, set it be 1 manually*/
 &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;first&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token keyword"&gt;id&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; start&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;1&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 21 Apr 2018 10:04:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Adding-start-and-stop-variables-based-on-number-of-days/m-p/456156#M14084</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-04-21T10:04:46Z</dc:date>
    </item>
  </channel>
</rss>

