<?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 do row calculation by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921892#M363046</link>
    <description>&lt;P&gt;If you want to preserve the original structure then you have to pass through each ID group twice, the first time to store the SCORE and STD for time 2 and time 3, the second pass to calculate a new SCORE and STD for time 1 if appropriate.&amp;nbsp; The data are output during the second pass:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID time score std;
cards;
111 1 . .
111 2 . .
111 3 7 5
222 1 4 6
222 2 5 7
222 3 4 8
333 1 . .
333 2 6 2
333 3 7 4
444 1 . .
444 2 6 4
444 3 7 2
run;

data want ;
  set have (in=firstpass)  have (in=secondpass);
  by id;
  array sc {2:3} _temporary_;
  array st {2:3} _temporary_;

  if first.id then call missing (of sc{*}, of st{*});
  if firstpass=1 and (2&amp;lt;=time&amp;lt;=3) then do;
    sc{time}=score;
    st{time}=std;
  end;

  if secondpass;
  if time=1 then do;
    if missing(score) and nmiss(of sc{*})=0 then score=sc{2}*sc{3};  
    if missing(std)   and nmiss(of st{*})=0 then std=st{2}*st{3};  
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 27 Mar 2024 01:11:11 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2024-03-27T01:11:11Z</dc:date>
    <item>
      <title>How to do row calculation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921847#M363014</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to do row calculation with the following rule:&lt;/P&gt;
&lt;P&gt;The calculation is done per ID.&lt;/P&gt;
&lt;P&gt;IF Y(Y= score, std) ) in time 1 is null and Y in time 2 and 3 are&amp;nbsp; not null then Y in time 1 will be equal to Y in time 2* time 3.&lt;/P&gt;
&lt;P&gt;Otherwise Y remain as is.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What is the way to do it please?&lt;/P&gt;
&lt;PRE&gt;Data have;
input ID time score std;
cards;
111 1 . .
111 2 . .
111 3 7 5
222 1 4 6
222 2 5 7
222 3 4 8
333 1 . .
333 2 6 2
333 3 7 4
444 1 . .
444 2 6 4
444 3 7 2
;
Run;&lt;/PRE&gt;
&lt;P&gt;expected output will looks like below:&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 192pt;" border="0" width="256" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD width="64" height="19" class="xl67" style="height: 14.5pt; width: 48pt;"&gt;ID&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="border-left: none; width: 48pt;"&gt;time&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="border-left: none; width: 48pt;"&gt;score&lt;/TD&gt;
&lt;TD width="64" class="xl67" style="border-left: none; width: 48pt;"&gt;std&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;111&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;111&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;111&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;5&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;222&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;222&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;5&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;222&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;333&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none;"&gt;42&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;333&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;6&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;333&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;444&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;1&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none;"&gt;42&lt;/TD&gt;
&lt;TD class="xl66" style="border-top: none; border-left: none;"&gt;8&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;444&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;6&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;4&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 14.5pt;"&gt;
&lt;TD height="19" class="xl65" style="height: 14.5pt; border-top: none;"&gt;444&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;3&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;7&lt;/TD&gt;
&lt;TD class="xl65" style="border-top: none; border-left: none;"&gt;2&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 18:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921847#M363014</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-03-26T18:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to do row calculation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921850#M363015</link>
      <description>&lt;P&gt;Data is not structed properly for that type of calculation, but that is easy to fix with PROC TRANSPOSE.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID time score std;
cards;
111 1 . .
111 2 . .
111 3 7 5
222 1 4 6
222 2 5 7
222 3 4 8
333 1 . .
333 2 6 2
333 3 7 4
444 1 . .
444 2 6 4
444 3 7 2
;

proc transpose data=have out=haveT name=MEASURE prefix=Y;
  by id;
  id time;
  var score std;
run;

proc print;
run;

data want;
  set havet ;
  y1 = coalesce(y1,y2*y3);
run;

proc print;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1711480527994.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/94951i3C26D1674E96D7E3/image-size/large?v=v2&amp;amp;px=999" role="button" title="Tom_0-1711480527994.png" alt="Tom_0-1711480527994.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Mar 2024 19:15:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921850#M363015</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-03-26T19:15:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to do row calculation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921858#M363020</link>
      <description>How can I change the data back to old arrangement?</description>
      <pubDate>Tue, 26 Mar 2024 19:59:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921858#M363020</guid>
      <dc:creator>stataq</dc:creator>
      <dc:date>2024-03-26T19:59:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to do row calculation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921892#M363046</link>
      <description>&lt;P&gt;If you want to preserve the original structure then you have to pass through each ID group twice, the first time to store the SCORE and STD for time 2 and time 3, the second pass to calculate a new SCORE and STD for time 1 if appropriate.&amp;nbsp; The data are output during the second pass:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID time score std;
cards;
111 1 . .
111 2 . .
111 3 7 5
222 1 4 6
222 2 5 7
222 3 4 8
333 1 . .
333 2 6 2
333 3 7 4
444 1 . .
444 2 6 4
444 3 7 2
run;

data want ;
  set have (in=firstpass)  have (in=secondpass);
  by id;
  array sc {2:3} _temporary_;
  array st {2:3} _temporary_;

  if first.id then call missing (of sc{*}, of st{*});
  if firstpass=1 and (2&amp;lt;=time&amp;lt;=3) then do;
    sc{time}=score;
    st{time}=std;
  end;

  if secondpass;
  if time=1 then do;
    if missing(score) and nmiss(of sc{*})=0 then score=sc{2}*sc{3};  
    if missing(std)   and nmiss(of st{*})=0 then std=st{2}*st{3};  
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Mar 2024 01:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921892#M363046</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2024-03-27T01:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to do row calculation by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921899#M363052</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID time score std;
cards;
111 1 . .
111 2 . .
111 3 7 5
222 1 4 6
222 2 5 7
222 3 4 8
333 1 . .
333 2 6 2
333 3 7 4
444 1 . .
444 2 6 4
444 3 7 2
;

data want;
 merge have 
       have(keep=ID score std rename=(id=id1 score=score1 std=std1) firstobs=2) 
       have(keep=ID score std rename=(id=id2 score=score2 std=std2) firstobs=3);
if ( id ne lag(id) and missing(score) and missing(std) )    and
   ( id eq id1 and not missing(score1) and not missing(std1) )   and
   ( id eq id2 and not missing(score2) and not missing(std2) ) then do; 
   score=score1*score2; std=std1*std2;
end;
drop id1 id2 score1 score2 std1 std2;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Mar 2024 02:24:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-do-row-calculation-by-group/m-p/921899#M363052</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2024-03-27T02:24:52Z</dc:date>
    </item>
  </channel>
</rss>

