<?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: calculate accumulate value of X-location of Retain in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/calculate-accumulate-value-of-X-location-of-Retain/m-p/729547#M227047</link>
    <description>&lt;P&gt;You don't need RETAIN at all, you can use a SUM statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set a;
    cum_x+x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 27 Mar 2021 12:39:15 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-03-27T12:39:15Z</dc:date>
    <item>
      <title>calculate accumulate value of X-location of Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-accumulate-value-of-X-location-of-Retain/m-p/729539#M227041</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to create a new column "Cum_X" that calculate accumulate value of X.&lt;/P&gt;
&lt;P&gt;I want to ask if the location of Retain is important?&lt;/P&gt;
&lt;P&gt;What is the common location of Retain ?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data a;
input Id X;
cards;
1 10 
2 30
3 20 
4 10
5 40
;
Run;

Data wanted1;
SET a;
Retain Cum_X;
IF _n_=1 then Cum_X=X;
Else Cum_X= Cum_X+X;
Run;

Data wanted2;
SET a;
IF _n_=1 then Cum_X=X;
Else Cum_X= Cum_X+X;
Retain Cum_X;
Run;

Data wanted3;
Retain Cum_X;
SET a;
IF _n_=1 then Cum_X=X;
Else Cum_X= Cum_X+X;
Run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Mar 2021 09:02:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-accumulate-value-of-X-location-of-Retain/m-p/729539#M227041</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2021-03-27T09:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: calculate accumulate value of X-location of Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-accumulate-value-of-X-location-of-Retain/m-p/729546#M227046</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The Type of the RETAIN statement is "Declarative".&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;A RETAIN statement is a declarative statement which is used in building the Program Data Vector (PDV) during the compilation phase of the DATA step.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;In your example I would put it right before or right after the SET statement. The difference is in the order of the variables in the output dataset.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;See this usage note for more insight:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Usage Note 8395: How to reorder the variables in a SAS® data set&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://support.sas.com/kb/8/395.html" target="_blank"&gt;https://support.sas.com/kb/8/395.html&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Have a nice day,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Koen&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 27 Mar 2021 11:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-accumulate-value-of-X-location-of-Retain/m-p/729546#M227046</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-03-27T11:30:12Z</dc:date>
    </item>
    <item>
      <title>Re: calculate accumulate value of X-location of Retain</title>
      <link>https://communities.sas.com/t5/SAS-Programming/calculate-accumulate-value-of-X-location-of-Retain/m-p/729547#M227047</link>
      <description>&lt;P&gt;You don't need RETAIN at all, you can use a SUM statement.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set a;
    cum_x+x;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 27 Mar 2021 12:39:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/calculate-accumulate-value-of-X-location-of-Retain/m-p/729547#M227047</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-03-27T12:39:15Z</dc:date>
    </item>
  </channel>
</rss>

