<?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 with do loop in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780574#M248738</link>
    <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;abc sum should be 1+0+2+0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;...&amp;nbsp;&lt;SPAN&gt;and xyz should sum should be 1+0&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not following this part of your explanation. Should not the abc sum be 1+0+2+0+2? Should not the xyx sum be 1+0+1+3?&lt;/P&gt;</description>
    <pubDate>Tue, 16 Nov 2021 20:43:59 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-11-16T20:43:59Z</dc:date>
    <item>
      <title>first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780564#M248734</link>
      <description>&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input var1 $ var2 var3;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;abc 4 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;abc 4 0&lt;/P&gt;&lt;P&gt;abc 4 2&lt;/P&gt;&lt;P&gt;abc 4 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;abc 4 2&lt;/P&gt;&lt;P&gt;xyz 2 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;xyz 2 0&lt;/P&gt;&lt;P&gt;xyz 2 1&lt;/P&gt;&lt;P&gt;xyz 2 3&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to calculate var3 sum by selecting # of observations based on var2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example: abc sum should be 1+0+2+0&amp;nbsp; and xyz should sum should be 1+0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use first.variable command with do loop and it is giving me an error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any other thoughts?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Nov 2021 20:39:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780564#M248734</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-11-16T20:39:24Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780571#M248736</link>
      <description>Did you have a question?</description>
      <pubDate>Tue, 16 Nov 2021 20:39:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780571#M248736</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-16T20:39:42Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780573#M248737</link>
      <description>&lt;P&gt;data test;&lt;/P&gt;&lt;P&gt;input var1 $ var2 var3;&lt;/P&gt;&lt;P&gt;datalines;&lt;/P&gt;&lt;P&gt;abc 4 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;abc 4 0&lt;/P&gt;&lt;P&gt;abc 4 2&lt;/P&gt;&lt;P&gt;abc 4 0&amp;nbsp;&lt;/P&gt;&lt;P&gt;abc 4 2&lt;/P&gt;&lt;P&gt;xyz 2 1&amp;nbsp;&lt;/P&gt;&lt;P&gt;xyz 2 0&lt;/P&gt;&lt;P&gt;xyz 2 1&lt;/P&gt;&lt;P&gt;xyz 2 3&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to calculate var3 sum by selecting # of observations based on var2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example: abc sum should be 1+0+2+0&amp;nbsp; and xyz should sum should be 1+0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use first.variable command with do loop and it is giving me an error.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any other thoughts?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Nov 2021 20:42:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780573#M248737</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-11-16T20:42:43Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780574#M248738</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;abc sum should be 1+0+2+0&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;...&amp;nbsp;&lt;SPAN&gt;and xyz should sum should be 1+0&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not following this part of your explanation. Should not the abc sum be 1+0+2+0+2? Should not the xyx sum be 1+0+1+3?&lt;/P&gt;</description>
      <pubDate>Tue, 16 Nov 2021 20:43:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780574#M248738</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-11-16T20:43:59Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780582#M248740</link>
      <description>&lt;P&gt;I don't think this is a data step process.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC MEANS, SQL are more appropriate.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You may need to play with the GROUP BY to get what you want but hard to see. For example I don't know why you're excluding 1/3, the last two records for XYZ.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select * , sum(var2) as var3
from test
group by var1, var2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;input var1 $ var2 var3;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;abc 4 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;abc 4 0&lt;/P&gt;
&lt;P&gt;abc 4 2&lt;/P&gt;
&lt;P&gt;abc 4 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;abc 4 2&lt;/P&gt;
&lt;P&gt;xyz 2 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;xyz 2 0&lt;/P&gt;
&lt;P&gt;xyz 2 1&lt;/P&gt;
&lt;P&gt;xyz 2 3&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to calculate var3 sum by selecting # of observations based on var2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example: abc sum should be 1+0+2+0&amp;nbsp; and xyz should sum should be 1+0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to use first.variable command with do loop and it is giving me an error.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any other thoughts?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;EDIT:&amp;nbsp;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/add_average_value_to_dataset.sas&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Nov 2021 21:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780582#M248740</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-16T21:11:50Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780590#M248743</link>
      <description>&lt;P&gt;It really helps to say exactly how "the sum is based on Var2".&lt;/P&gt;
&lt;P&gt;I am going to &lt;STRONG&gt;guess&lt;/STRONG&gt; that you mean is that the "Var2 has the number of records to sum over for Var3".&lt;/P&gt;
&lt;P&gt;Which brings up the question of exactly what your expected output would look like. One row for Var1='abc'? the current 5 rows but the sum appearing only on the 4th? Last? All the ABC records?&lt;/P&gt;
&lt;P&gt;One way that may get started:&lt;/P&gt;
&lt;PRE&gt;data possible;
  set test;
  by notsorted var1;
  retain loopcount sumtot;
  if first.var1 then do;
      loopcount=0;
      sumtot=0;
  end;
  loopcount+1;
  sumtot= sum(sumtot,(loopcount le var2)*var3);
  if last.var1;
  drop loopcount;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you expect to happen if Var2 is bigger than than the number of records for the values of Var1?&lt;/P&gt;
&lt;P&gt;How would missing values in Var3 be handled?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/324991"&gt;@dht115&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;data test;&lt;/P&gt;
&lt;P&gt;input var1 $ var2 var3;&lt;/P&gt;
&lt;P&gt;datalines;&lt;/P&gt;
&lt;P&gt;abc 4 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;abc 4 0&lt;/P&gt;
&lt;P&gt;abc 4 2&lt;/P&gt;
&lt;P&gt;abc 4 0&amp;nbsp;&lt;/P&gt;
&lt;P&gt;abc 4 2&lt;/P&gt;
&lt;P&gt;xyz 2 1&amp;nbsp;&lt;/P&gt;
&lt;P&gt;xyz 2 0&lt;/P&gt;
&lt;P&gt;xyz 2 1&lt;/P&gt;
&lt;P&gt;xyz 2 3&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to calculate var3 sum by selecting # of observations based on var2.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example: abc sum should be 1+0+2+0&amp;nbsp; and xyz should sum should be 1+0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I tried to use first.variable command with do loop and it is giving me an error.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any other thoughts?&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Nov 2021 21:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780590#M248743</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-11-16T21:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780592#M248744</link>
      <description>&lt;P&gt;no - because var2 value for xyz variable is 2. Therefore we need to select first two observation for xyz.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to use use first.variable option. I am able to get expected result by producing sum for all observation by group.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am able to produce the result but not able to get the desire result by selecting number of the observation based on var2.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA test1;&lt;/P&gt;&lt;P&gt;set test;&lt;/P&gt;&lt;P&gt;by var1;&lt;/P&gt;&lt;P&gt;if first.var1 then do;&lt;/P&gt;&lt;P&gt;final = var3;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else do;&lt;/P&gt;&lt;P&gt;retain final;&lt;/P&gt;&lt;P&gt;final = var3 + final;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I choose first.variable option because I also need to calculate other function as well beside sum.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to do following logic and it is not providing me desire output&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;DATA test1;&lt;/P&gt;&lt;P&gt;set test;&lt;/P&gt;&lt;P&gt;by var1;&lt;/P&gt;&lt;P&gt;if first.var1 then do;&lt;/P&gt;&lt;P&gt;final = var3;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;else do i = 1 to var2;&lt;/P&gt;&lt;P&gt;retain final;&lt;/P&gt;&lt;P&gt;final = var3 + final;&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Nov 2021 21:37:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780592#M248744</guid>
      <dc:creator>dht115</dc:creator>
      <dc:date>2021-11-16T21:37:35Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780597#M248748</link>
      <description>You win the guessing games today!</description>
      <pubDate>Tue, 16 Nov 2021 21:40:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780597#M248748</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-11-16T21:40:30Z</dc:date>
    </item>
    <item>
      <title>Re: first. Variable with do loop</title>
      <link>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780623#M248750</link>
      <description>&lt;P&gt;Just count how many observations you have processed for this value of your ID variable (VAR1).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  do obs=1 by 1 until (last.var1);
    set test ;
    by var1 ;
    if obs &amp;lt;= var2 then want=sum(want,var3);
  end;
  keep var1 want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;PRE&gt;Obs    var1    want

 1     abc       3
 2     xyz       1

&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Nov 2021 23:45:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/first-Variable-with-do-loop/m-p/780623#M248750</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-11-16T23:45:35Z</dc:date>
    </item>
  </channel>
</rss>

