<?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: Sum on a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429607#M106124</link>
    <description>&lt;P&gt;You apply the same basic technique of renaming/dropping the old variable, retaining the new one, and use an addition instead of the catx function. At first.creance, set the new variable to zero.&lt;/P&gt;</description>
    <pubDate>Mon, 22 Jan 2018 13:37:12 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-01-22T13:37:12Z</dc:date>
    <item>
      <title>Sum on a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429597#M106119</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have some trouble.I have one table( here person)&lt;/P&gt;
&lt;PRE&gt;data person;&lt;BR /&gt; infile datalines delimiter=','; &lt;BR /&gt; input CREANCE $14. POND REPOR RISK $ COUNTRY$;&lt;BR /&gt; datalines; &lt;BR /&gt;20241514156EUR,272186, 136093,ART122, FR&lt;BR /&gt;20241514156EUR,272186, 136093,ART122, NO&lt;BR /&gt;20085273456EUR,341928, 65688,ART126-b, FR&lt;BR /&gt;20085273456EUR,341928, 170964,ART126-a, FR&lt;BR /&gt;;&lt;/PRE&gt;
&lt;P&gt;and i would like to have this one in result&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data person1;&lt;BR /&gt; infile datalines delimiter=','; &lt;BR /&gt; input CREANCE $14. POND REPOR RISK $ COUNTRY$;&lt;BR /&gt; datalines; &lt;BR /&gt;20241514156EUR,272186, 136093, ART122-ART122,FR-NO&lt;BR /&gt;20085273456EUR,341928, 236652, ART126-b-ART126-a,FR-FR&lt;BR /&gt;;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;Here the first column are equal and the last column are not equal, so &amp;nbsp;i keep the first, second, third column, concatenate the fourth and fifth column.&lt;BR /&gt;
&lt;PRE&gt;20241514156EUR,272186, 136093,ART122, FR&lt;BR /&gt;20241514156EUR,272186, 136093,ART122, NO&lt;/PRE&gt;
&lt;/LI&gt;
&lt;LI&gt;&lt;CODE class=" language-sas"&gt;For others, first colum are identical the same as the last column, so i keep the first, second column, sum the third column, concatenate the fourth and fiht column.&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;
&lt;PRE&gt;20085273456EUR,341928, 65688,ART126-b, FR&lt;BR /&gt;20085273456EUR,341928, 170964,ART126-a, FR&lt;/PRE&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jan 2018 13:01:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429597#M106119</guid>
      <dc:creator>foxrol94</dc:creator>
      <dc:date>2018-01-22T13:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Sum on a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429599#M106120</link>
      <description>&lt;P&gt;Use retain, by-group processing, and the catx() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person;
 infile datalines delimiter=','; 
 input CREANCE $14. POND REPOR RISK $ COUNTRY$;
 datalines; 
20241514156EUR,272186, 136093,ART122, FR
20241514156EUR,272186, 136093,ART122, NO
20085273456EUR,341928, 65688,ART126-b, FR
20085273456EUR,341928, 170964,ART126-a, FR
;
run;

data person1;
set person (rename=(risk=_risk country=_country));
by creance notsorted;
retain
  risk
  country
;
length
  risk $100
  country $20
;
if first.creance
then do;
  risk = _risk;
  country = _country;
end;
else do;
  risk = catx('-',risk,_risk);
  country = catx('-',country,_country);
end;
if last.creance then output;
drop _risk _country;
run;

proc print data=person1 noobs;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;   CREANCE         POND      REPOR          risk           country

20241514156EUR    272186    136093    ART122-ART122         FR-NO 
20085273456EUR    341928    170964    ART126-b-ART126-a     FR-FR 
&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jan 2018 13:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429599#M106120</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-22T13:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: Sum on a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429603#M106122</link>
      <description>&lt;P&gt;As an alternative, you could transpose and cat:&lt;/P&gt;
&lt;PRE&gt;proc transpose data=person out=inter;
  by creance pond repor risk;
  var country;
run;

data want (drop=var:);
  set inter;
  length country $2000;
  country=catx("-",of var:);
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 22 Jan 2018 13:30:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429603#M106122</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-22T13:30:49Z</dc:date>
    </item>
    <item>
      <title>Re: Sum on a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429605#M106123</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;the value of "repor" has to be &lt;U&gt;236652&lt;/U&gt; in the last observation ,&amp;nbsp;&amp;nbsp;please suggest a way for that also&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jan 2018 13:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429605#M106123</guid>
      <dc:creator>soham_sas</dc:creator>
      <dc:date>2018-01-22T13:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: Sum on a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429607#M106124</link>
      <description>&lt;P&gt;You apply the same basic technique of renaming/dropping the old variable, retaining the new one, and use an addition instead of the catx function. At first.creance, set the new variable to zero.&lt;/P&gt;</description>
      <pubDate>Mon, 22 Jan 2018 13:37:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429607#M106124</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-22T13:37:12Z</dc:date>
    </item>
    <item>
      <title>Re: Sum on a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429922#M106228</link>
      <description>&lt;P&gt;Hi KurtBremser,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As you suggested i've modified and get this code&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person1;
set person (rename=(risk=_risk country=_country repor=_repor));
by creance notsorted;
retain
  repor
  risk
  country
;
length
  risk $100
  country $20
;
if first.creance
then do;
  repor = 0;
  risk = _risk;
  country = _country;
end;
else do;
  repor = repor + _repor;
  risk = catx('-',risk,_risk);
  country = catx('-',country,_country);
end;
if last.creance then output;
drop _risk _country _repor;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and i get this output&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;     CREANCE           POND      repor          risk           country

     20241514156EUR    272186    136093    ART122-ART122         FR-NO
     20085273456EUR    341928    170964    ART126-b-ART126-a     FR-FR
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;As you see the last line is not the one i would expect.&lt;/P&gt;
&lt;P&gt;Please could you tell me what to do.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 09:29:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429922#M106228</guid>
      <dc:creator>foxrol94</dc:creator>
      <dc:date>2018-01-23T09:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: Sum on a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429925#M106229</link>
      <description>&lt;P&gt;You made a slight mistake in the initializing of repor:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.creance
then do;
  repor = 0;
  risk = _risk;
  country = _country;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;should be&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.creance
then do;
  repor = _repor;
  risk = _risk;
  country = _country;
end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;otherwise you're missing the first value of a group.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 09:39:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-on-a-data-step/m-p/429925#M106229</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-23T09:39:03Z</dc:date>
    </item>
  </channel>
</rss>

