<?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: How to collapse in data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284221#M57992</link>
    <description>&lt;P&gt;The results that you described do not keep the sums in the data set. &amp;nbsp;You sketched out a separate data set. &amp;nbsp;What would you like the result to look like?&lt;/P&gt;</description>
    <pubDate>Wed, 13 Jul 2016 23:35:49 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2016-07-13T23:35:49Z</dc:date>
    <item>
      <title>How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284218#M57990</link>
      <description>&lt;P&gt;Hi, I am a new SAS user and I use SAS 9.4. I have a data set like this:&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;numbers&lt;/P&gt;&lt;P&gt;1001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/P&gt;&lt;P&gt;1001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1 &amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1002 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1002 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1003 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1003 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1003 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;...&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I hope to collapse like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;id &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;sum_numbers&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1001 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 4&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1002 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1003 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 3&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I searched some info and I used the following codes which works:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;PROC MEANS DATA=have;&lt;BR /&gt;CLASS id;&lt;BR /&gt;VAR numbers;&lt;BR /&gt;OUTPUT OUT=have2 sum= ;&lt;BR /&gt;RUN;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, it is not in the data set anymore. SAS gives me another seperate result table in a seperate window. So I am wondering if there is any magic code in the data step that can keep the results still as part of the dataset?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 23:22:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284218#M57990</guid>
      <dc:creator>Therain</dc:creator>
      <dc:date>2016-07-13T23:22:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284221#M57992</link>
      <description>&lt;P&gt;The results that you described do not keep the sums in the data set. &amp;nbsp;You sketched out a separate data set. &amp;nbsp;What would you like the result to look like?&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 23:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284221#M57992</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-07-13T23:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284222#M57993</link>
      <description>&lt;P&gt;SQL can. It essentially requires merging the original with the summary data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will generate a message in the log. You can do it in a data step but the code is more complex.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sql;&lt;/P&gt;
&lt;P&gt;create table want as&lt;/P&gt;
&lt;P&gt;select *, sum(numbers) as sum_numbers&lt;/P&gt;
&lt;P&gt;from have&lt;/P&gt;
&lt;P&gt;group by id;&lt;/P&gt;
&lt;P&gt;quit.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 23:39:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284222#M57993</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-13T23:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284223#M57994</link>
      <description>&lt;P&gt;SQL can. It essentially requires merging the original with the summary data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will generate a message in the log. You can do it in a data step but the code is more complex.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sql;&lt;/P&gt;
&lt;P&gt;create table want as&lt;/P&gt;
&lt;P&gt;select *, sum(numbers) as sum_numbers&lt;/P&gt;
&lt;P&gt;from have&lt;/P&gt;
&lt;P&gt;group by id;&lt;/P&gt;
&lt;P&gt;quit.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 13 Jul 2016 23:39:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284223#M57994</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-07-13T23:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284228#M57998</link>
      <description>Hi, thanks for the reply! It could be a separate data set or in the same data set. Both are OK.</description>
      <pubDate>Thu, 14 Jul 2016 00:06:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284228#M57998</guid>
      <dc:creator>Therain</dc:creator>
      <dc:date>2016-07-14T00:06:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284229#M57999</link>
      <description>It works!&lt;BR /&gt;Thank you very much!</description>
      <pubDate>Thu, 14 Jul 2016 00:11:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284229#M57999</guid>
      <dc:creator>Therain</dc:creator>
      <dc:date>2016-07-14T00:11:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284340#M58029</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datas;
	input Id numbers;
datalines;
1001            1
1001            1
1001            .
1001            1
1002            1
1002            .
1003            1
1003            1
1003            1
run;
proc sort data=datas
 out=datas;
 by Id;
 run;
data sumbygroup (drop=numbers);
set datas;
by Id;
if first.Id then do; sum=numbers; count=1;end;
			else do; sum+numbers; count+1;end;
if last.Id then output;
run;

proc print data=sumbygroup;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jul 2016 14:07:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284340#M58029</guid>
      <dc:creator>ctrlx</dc:creator>
      <dc:date>2016-07-14T14:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to collapse in data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284403#M58053</link>
      <description>Thank you! It works as well!</description>
      <pubDate>Thu, 14 Jul 2016 17:38:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-collapse-in-data-step/m-p/284403#M58053</guid>
      <dc:creator>Therain</dc:creator>
      <dc:date>2016-07-14T17:38:05Z</dc:date>
    </item>
  </channel>
</rss>

