<?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: First.variable and Last.Variable initial value before execution in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229041#M41410</link>
    <description>&lt;P&gt;It matters when using it in loop. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
store_id=101;
sale_eff_date = '01may2015'd;
sale_end_date = '15may2015'd;
total_amount = 100;
output;
store_id=101;
sale_eff_date = '17may2015'd;
sale_end_date = '30may2015'd;
total_amount = 200;
output;
store_id=102;
sale_eff_date = '17may2015'd;
sale_end_date = '30may2015'd;
total_amount = 200;
output;
format sale_eff_date sale_end_date  mmddyy10.;
run;


data temp1;
put "before exec " _all_;
do until(last.store_id);
set temp;
by store_id;
total = sum(total,total_amount);
first_sd=first.store_id;
last_id=last.store_id;
output;
put "after exec " _all_;
end;
run;

data temp2;
put "before exec " _all_;
do while(last.store_id);
set temp;
by store_id;
total = sum(total,total_amount);
first_sd=first.store_id;
last_id=last.store_id;
output;
put "after exec " _all_;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 07 Oct 2015 23:57:36 GMT</pubDate>
    <dc:creator>nkm1</dc:creator>
    <dc:date>2015-10-07T23:57:36Z</dc:date>
    <item>
      <title>First.variable and Last.Variable initial value before execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229005#M41401</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it normal to set initial value of first. and last. to 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data temp;&lt;BR /&gt;store_id=101;&lt;BR /&gt;sale_eff_date = '01may2015'd;&lt;BR /&gt;sale_end_date = '15may2015'd;&lt;BR /&gt;total_amount = 100;&lt;BR /&gt;output;&lt;BR /&gt;sale_eff_date=101;&lt;BR /&gt;sale_eff_date = '17may2015'd;&lt;BR /&gt;sale_end_date = '30may2015'd;&lt;BR /&gt;total_amount = 200;&lt;BR /&gt;output;&lt;BR /&gt;format sale_eff_date sale_end_date mmddyy10.;&lt;BR /&gt;run;&lt;BR /&gt;data temp1;&lt;BR /&gt;put "before exec " _all_;&lt;BR /&gt;set temp;&lt;BR /&gt;by store_id;&lt;BR /&gt;put "after exec " _all_;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;before exec store_id=. sale_eff_date=. sale_end_date=. total_amount=. FIRST.store_id=1 LAST.store_id=1 _ERROR_=0 _N_=1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;after exec store_id=101 sale_eff_date=05/01/2015 sale_end_date=05/15/2015 total_amount=100 FIRST.store_id=1 LAST.store_id=0&amp;nbsp;_ERROR_=0 _N_=1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Why first.store_id and last.store_id is set to 1 before execution. Isn't that should be set to missing ? Why SAS set initial value to 1 for first. and last.variable ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2015 20:22:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229005#M41401</guid>
      <dc:creator>nkm1</dc:creator>
      <dc:date>2015-10-07T20:22:53Z</dc:date>
    </item>
    <item>
      <title>Re: First.variable and Last.Variable initial value before execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229019#M41405</link>
      <description>&lt;P&gt;Here's a fun experiment&amp;nbsp;to try ...&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create empty dataset */
data temp2;
  var1 = 0;
  delete;
run;

data temp3;
  put "before exec " _all_;
  set temp2;
  by var1;
  put "after exec " _all_;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log output:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;18         /* create empty dataset */
19         data temp2;
20           var1 = 0;
21           delete;
22         run;

&lt;FONT color="#339966"&gt;NOTE: The data set WORK.TEMP2 has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds&lt;/FONT&gt;
      
23         
24         data temp3;
25           put "before exec " _all_;
26           set temp2;
27           by var1;
28           put "after exec " _all_;
29         run;

before exec var1=. FIRST.var1=1 LAST.var1=1 _ERROR_=0 _N_=1
&lt;FONT color="#339966"&gt;NOTE: There were 0 observations read from the data set WORK.TEMP2.
NOTE: The data set WORK.TEMP3 has 0 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2015 21:52:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229019#M41405</guid>
      <dc:creator>hbi</dc:creator>
      <dc:date>2015-10-07T21:52:58Z</dc:date>
    </item>
    <item>
      <title>Re: First.variable and Last.Variable initial value before execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229032#M41408</link>
      <description>&lt;P&gt;First and Last variables are flags and boolean in their nature. They should only have two values (True, False).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You have your put statement before the SET so I would assume what you get is how SAS initializes these temporay variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;It's interesting to know to what SAS sets the values during initialization but I can't think of any use case where this really matters or would cause issues.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Oct 2015 22:40:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229032#M41408</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2015-10-07T22:40:28Z</dc:date>
    </item>
    <item>
      <title>Re: First.variable and Last.Variable initial value before execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229041#M41410</link>
      <description>&lt;P&gt;It matters when using it in loop. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp;
store_id=101;
sale_eff_date = '01may2015'd;
sale_end_date = '15may2015'd;
total_amount = 100;
output;
store_id=101;
sale_eff_date = '17may2015'd;
sale_end_date = '30may2015'd;
total_amount = 200;
output;
store_id=102;
sale_eff_date = '17may2015'd;
sale_end_date = '30may2015'd;
total_amount = 200;
output;
format sale_eff_date sale_end_date  mmddyy10.;
run;


data temp1;
put "before exec " _all_;
do until(last.store_id);
set temp;
by store_id;
total = sum(total,total_amount);
first_sd=first.store_id;
last_id=last.store_id;
output;
put "after exec " _all_;
end;
run;

data temp2;
put "before exec " _all_;
do while(last.store_id);
set temp;
by store_id;
total = sum(total,total_amount);
first_sd=first.store_id;
last_id=last.store_id;
output;
put "after exec " _all_;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Oct 2015 23:57:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229041#M41410</guid>
      <dc:creator>nkm1</dc:creator>
      <dc:date>2015-10-07T23:57:36Z</dc:date>
    </item>
    <item>
      <title>Re: First.variable and Last.Variable initial value before execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229076#M41415</link>
      <description>&lt;P&gt;It may matter then, but then the reason is why are you doing it that way in the first place? &amp;nbsp;To attain the end result, this would be the way to code it, with a retain statement:&lt;/P&gt;
&lt;PRE&gt;data temp1;
  set temp;
  by store_id;
  retain total;
  if first.store_id then total=0;
  total=sum(total,total_amount);
run;&lt;/PRE&gt;</description>
      <pubDate>Thu, 08 Oct 2015 08:55:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229076#M41415</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2015-10-08T08:55:20Z</dc:date>
    </item>
    <item>
      <title>Re: First.variable and Last.Variable initial value before execution</title>
      <link>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229140#M41430</link>
      <description>&lt;P&gt;Hi, you could also use a SUM statement and make use of the automatic retain of variables created in that statement (plus initialization at zero, not missing).&amp;nbsp; The following makes use of the value of FIRST.STORE_ID ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;data temp;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;input store_id total_amount @@;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;datalines;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;1 10 1 30 1 50 2 40 2 70 2 90 3 10 3 20&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;data temp1;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp; set temp;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp; by store_id;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp; total + total_amount - (first.store_id * total);&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;run;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data set TEMP1 ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&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; total_&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;Obs&amp;nbsp;&amp;nbsp;&amp;nbsp; store_id&amp;nbsp;&amp;nbsp;&amp;nbsp; amount&amp;nbsp;&amp;nbsp;&amp;nbsp; total&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 40&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 90&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 40&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 40&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 70&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 110&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 90&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 200&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 10&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;&amp;nbsp;8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 20&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 30&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="arial,helvetica,sans-serif"&gt;&lt;STRONG&gt;Tips:Between and Within Group Counters&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://www.sascommunity.org/wiki/Tips:Between_and_Within_Group_Counters" target="_blank"&gt;http://www.sascommunity.org/wiki/Tips:Between_and_Within_Group_Counters&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 08 Oct 2015 15:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/First-variable-and-Last-Variable-initial-value-before-execution/m-p/229140#M41430</guid>
      <dc:creator>MikeZdeb</dc:creator>
      <dc:date>2015-10-08T15:03:27Z</dc:date>
    </item>
  </channel>
</rss>

