<?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: Was the variable &amp;quot;RunsToDate&amp;quot; a new created variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444526#M282944</link>
    <description>&lt;P&gt;The retained variables are not stored in a different section of memory, they are part of the PDV like all other non-automatic variables, but the data step always does this with variables in the PDV:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;variables from input datasets are retained (so when one observation of dataset A is merged with several observations of B, the values of A persist)&lt;/LI&gt;
&lt;LI&gt;newly created variables are always set to missing at the start of a new datastep iteration, unless they are named in a retain statement or a summation statement of the form&amp;nbsp;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x + n;&lt;/CODE&gt;&lt;/PRE&gt;
(x will be retained, n is any numeric expression)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;HTH&lt;/P&gt;</description>
    <pubDate>Sun, 11 Mar 2018 08:58:07 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-03-11T08:58:07Z</dc:date>
    <item>
      <title>Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444419#M282938</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;this topic was from my assignment,&amp;nbsp;&lt;/P&gt;&lt;P&gt;and after checking the answer provided,&lt;/P&gt;&lt;P&gt;I still had this question.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data-set is as below:&lt;/P&gt;&lt;P&gt;The labels are:&lt;/P&gt;&lt;P&gt;Month Date Team Hits Runs Status&lt;/P&gt;&lt;PRE&gt;6-19 Columbia Peaches      8  3 Complete
6-20 Columbia Peaches     10  5 Complete
6-23 Plains Peanuts        3  4 Complete
6-24 Plains Peanuts        7  2 Complete
6-25 Plains Peanuts       12  8 Complete
6-30 Gilroy Garlics        .  . No Data
7-1  Gilroy Garlics        .  . No Data
7-4  Sacramento Tomatoes  15  9 Complete
7-4  Sacramento Tomatoes  10 10 Complete
7-5  Sacramento Tomatoes   2  3 Complete&lt;/PRE&gt;&lt;P&gt;The question was:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;You want to accumulate the maximum number of runs &lt;EM&gt;that you know about&lt;/EM&gt;. In this case, for example, record 6 should list MaxRuns=8.&lt;/LI&gt;&lt;LI&gt;You only want to accumulated the total number of runs to date until you don’t have information—when this happens you want to set RunsToDate to a missing value. In this case, for example, record 6 should list RunsToDate=.;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;The code was as below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data mydata;
infile "&amp;amp;dirdata/Week_5/Games_Plus.dat" truncover;
input Month 1 Day 3-4 Team $6-24 Hits 27-28 Runs 30-31 Status $9.;
retain MaxRuns RunstoDate 0 ;
MaxRuns=Max(MaxRuns, Runs);
RunsToDate=RunsToDate+Runs;
run;

proc print data=mydata;
title "Season's Record to Date, with Missing Values";
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My question was the "Retain" command:&lt;/P&gt;&lt;P&gt;Starting from here, I had not yet created variables named "MaxRuns" and "RunsToDate".&lt;/P&gt;&lt;P&gt;However, it seemed SAS knows this.&lt;/P&gt;&lt;P&gt;And I also did not understand MaxRuns: why did it state as Max(MaxRuns,Runs) instead of simply MaxRuns=Max(Runs)&lt;/P&gt;&lt;P&gt;And I think "RunsToDate=RunsToDate+Runs" is because it is like RunsToDate of record 3=RunsToDate of record 2 +Runs of record 3 and so on...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I guess I did not really understand about this question,&lt;/P&gt;&lt;P&gt;I wonder if anyone understand this and would like to guiding me a little bit.&lt;/P&gt;&lt;P&gt;Thanks a lot!:)&lt;/P&gt;</description>
      <pubDate>Sat, 10 Mar 2018 17:20:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444419#M282938</guid>
      <dc:creator>jc3992</dc:creator>
      <dc:date>2018-03-10T17:20:43Z</dc:date>
    </item>
    <item>
      <title>Re: Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444449#M282939</link>
      <description>&lt;P&gt;Contrary to SQL, where a summary function like max() can work over all rows, a data step (and a data step function) always deals with the current observation only. So you need to compare the current value with the retained summary value.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Mar 2018 20:21:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444449#M282939</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-10T20:21:09Z</dc:date>
    </item>
    <item>
      <title>Re: Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444465#M282940</link>
      <description>&lt;P&gt;The retain statement can precede the corresponding value assignment statement (although the retain statement has the option of assigning in initial value.&amp;nbsp;&amp;nbsp;Note because of this, you can declare a retained variable even though not only the value, but also the variable type (numeric vs character) is not evident until a subsequent statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Mar 2018 21:23:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444465#M282940</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-10T21:23:32Z</dc:date>
    </item>
    <item>
      <title>Re: Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444498#M282941</link>
      <description>&lt;P&gt;Thank you. A bit complicated but I think I will figure it out after I am more familiar to it. Thanks!&lt;/P&gt;</description>
      <pubDate>Sun, 11 Mar 2018 02:16:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444498#M282941</guid>
      <dc:creator>jc3992</dc:creator>
      <dc:date>2018-03-11T02:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444499#M282942</link>
      <description>&lt;P&gt;Thank you very much! Now I understand. Thanks~&lt;/P&gt;</description>
      <pubDate>Sun, 11 Mar 2018 02:17:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444499#M282942</guid>
      <dc:creator>jc3992</dc:creator>
      <dc:date>2018-03-11T02:17:03Z</dc:date>
    </item>
    <item>
      <title>Re: Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444509#M282943</link>
      <description>&lt;P&gt;Think of it this way.&amp;nbsp; When the SAS data step encounters a retain statement it turns out that the retained variables&amp;nbsp;are stored in a different part of memory than for&amp;nbsp;non-retained variables.&amp;nbsp; This can be demonstrated by use of the ADDRLONG function, which I don't propose to describe here.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in a way, all the retain statement apparently does is assign&amp;nbsp;a variable's location in memory to a region which the data step does&amp;nbsp;not reset to missing with each new record.&lt;/P&gt;</description>
      <pubDate>Sun, 11 Mar 2018 03:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444509#M282943</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-11T03:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444526#M282944</link>
      <description>&lt;P&gt;The retained variables are not stored in a different section of memory, they are part of the PDV like all other non-automatic variables, but the data step always does this with variables in the PDV:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;variables from input datasets are retained (so when one observation of dataset A is merged with several observations of B, the values of A persist)&lt;/LI&gt;
&lt;LI&gt;newly created variables are always set to missing at the start of a new datastep iteration, unless they are named in a retain statement or a summation statement of the form&amp;nbsp;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x + n;&lt;/CODE&gt;&lt;/PRE&gt;
(x will be retained, n is any numeric expression)&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;HTH&lt;/P&gt;</description>
      <pubDate>Sun, 11 Mar 2018 08:58:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444526#M282944</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-03-11T08:58:07Z</dc:date>
    </item>
    <item>
      <title>Re: Was the variable "RunsToDate" a new created variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444560#M282945</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Retained variables&amp;nbsp;are indeed part of the pdv, but that does not mean they have the same memory address as when they are not retained.&amp;nbsp; This is what I meant by "section" of memory.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For simplicity let me restrict my example to numeric variables that are newly created in the data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider the impact on the address of variable W below.&amp;nbsp; W is retained in the second data step but not the first, and&amp;nbsp;as a result has a different memory address.&amp;nbsp;&amp;nbsp;&amp;nbsp;In fact, if you have a number of new variables, and retain a subset, I have never seen&amp;nbsp;a retained variable in a memory location contiguous to the non-retained vars.&amp;nbsp; Instead they are contiguous to each other (separated by 8 bytes needed for numeric variables).&amp;nbsp;&amp;nbsp; And the non-retained vars are similarly contiguous to each other.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is why I believe it is a useful paradigm to consider the retain statement as a memory-location assignment statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Even so, as you have noted, they are in the PDV, and non-retained and retained variables can be logically contiguous - handy for programming logic statement, such as array declarations, etc.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   set sashelp.class;
   a=age;
   w=weight;
   h=height;

   ada=addrlong(a);
   adw=addrlong(w);
   adh=addrlong(h);
   put (ad:) (=$hex16. /);
   stop;
run;

data _null_;
   set sashelp.class;
   a=age;
   w=weight;
   h=height;
   retain w;
   ada=addrlong(a);
   adw=addrlong(w);
   adh=addrlong(h);
   put (ad:) (=$hex16. /);
   stop;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My system is windows, which has "little endian" addresses, so addresses&amp;nbsp;such as&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; 6840750600000000&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; 7040750600000000&lt;/P&gt;
&lt;P&gt;&lt;LI-WRAPPER&gt;&lt;/LI-WRAPPER&gt;&lt;/P&gt;
&lt;P&gt;are "contiguous"&amp;nbsp; (ie&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6840750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6940750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6A40750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6B40750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6C40750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6D40750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6E40750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 6F40750600000000&amp;nbsp; is followed by&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; location 7040750600000000&lt;/P&gt;
&lt;P&gt;providing 8 bytes for the numeric variable at the first address.&lt;/P&gt;</description>
      <pubDate>Sun, 11 Mar 2018 16:41:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Was-the-variable-quot-RunsToDate-quot-a-new-created-variable/m-p/444560#M282945</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-03-11T16:41:47Z</dc:date>
    </item>
  </channel>
</rss>

