<?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: Array from first row of a group to be a fraction of the last row of the previous group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958087#M373958</link>
    <description>&lt;P&gt;Can you show an actual completed example of what you expect to create? I can't follow what you have described.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
set have;by group;
array num _numeric_; /* range over the numeric values*/
if group = '2' then do; /* go to group 2*/
	if first.group then do; /* go to first observation of group 2*/ &lt;FONT size="5" color="#FF00FF"&gt;&lt;STRONG&gt;&amp;lt;= this comment indicates misunderstanding of what FIRST&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="5" color="#FF00FF"&gt;&lt;STRONG&gt;                                                                           or LAST variables are&lt;/STRONG&gt;&lt;/FONT&gt;
		do over num;
			format num best6.2;
			num = 0.95*lag(num); /* first row of group 2 set to 95% of last row of previous group*/
		end;

	else do;
		do over num; /* remainder rows of 2nd group*/
			num = lag(num); /* all subsequent rows of group 2 equal to 1st row of group 2*/
		end;
	end;
end;
run;&lt;/PRE&gt;</description>
    <pubDate>Mon, 03 Feb 2025 19:07:44 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2025-02-03T19:07:44Z</dc:date>
    <item>
      <title>Array from first row of a group to be a fraction of the last row of the previous group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958042#M373945</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'd like to set a numeric array in all rows of a second group to be a fraction of the corresponding values in last row of the previous group.&amp;nbsp; Code-wise, this is the (obviously flawed) program I'm trying to write:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;

infile datalines delimiter =',';

input Group :$1. val1 val2 :3.;

datalines;
1,5,8
1,4,10
1,7,11
2,7,1
2,5,9
;
run;

data want;
set have;&lt;BR /&gt;by group;
array num _numeric_; /* range over the numeric values*/
if group = '2' then do; /* go to group 2*/
	if first.group then do; /* go to first observation of group 2*/
		do over num;
			format num best6.2;
			num = 0.95*lag(num); /* first row of group 2 set to 95% of last row of previous group*/
		end;

	else do;
		do over num; /* remainder rows of 2nd group*/
			num = lag(num); /* all subsequent rows of group 2 equal to 1st row of group 2*/
		end;
	end;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I've come up with a roundabout way of doing it, but I'm sure there will be a quick fix. Please post any such you can come up with.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 16:12:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958042#M373945</guid>
      <dc:creator>RDzh</dc:creator>
      <dc:date>2025-02-03T16:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: Array from first row of a group to be a fraction of the last row of the previous group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958068#M373953</link>
      <description>&lt;P&gt;You need to be much more specific about what you want.&lt;/P&gt;
&lt;P&gt;The way you described it you appear to want to modify the values in observation 4 only (the first observation of the second group).&lt;/P&gt;
&lt;P&gt;What happens in observation 5 (the second observation for the second group)?&lt;/P&gt;
&lt;P&gt;What happens in observations 1 to 3 (the first group)?&lt;/P&gt;
&lt;P&gt;What happens in the third group?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Calling LAG conditionally is going to cause a lot of confusion. LAG returns the values you have previously passed it.&amp;nbsp; If you don't call LAG() on the last observation of the previous group then there is no way for it know how to return that value later.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This seems to be what described.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Group $ val1 val2 ;
datalines;
1 5 8
1 4 10
1 7 11
2 7 1
2 5 9
;

data want;
  set have;
  by group;
  retain lag1 lag2 ;
  if last.group then do; lag1 = val1; lag2 = val2; end;
  else if first.group and _n_&amp;gt;1 then do;
    val1 = lag1 * .95 ;
    val2 = lag2 * .95 ;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Which gives this result:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Tom_0-1738603088562.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104273i5A84ABC9BDADD157/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Tom_0-1738603088562.png" alt="Tom_0-1738603088562.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 17:18:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958068#M373953</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-03T17:18:17Z</dc:date>
    </item>
    <item>
      <title>Re: Array from first row of a group to be a fraction of the last row of the previous group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958087#M373958</link>
      <description>&lt;P&gt;Can you show an actual completed example of what you expect to create? I can't follow what you have described.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
set have;by group;
array num _numeric_; /* range over the numeric values*/
if group = '2' then do; /* go to group 2*/
	if first.group then do; /* go to first observation of group 2*/ &lt;FONT size="5" color="#FF00FF"&gt;&lt;STRONG&gt;&amp;lt;= this comment indicates misunderstanding of what FIRST&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT size="5" color="#FF00FF"&gt;&lt;STRONG&gt;                                                                           or LAST variables are&lt;/STRONG&gt;&lt;/FONT&gt;
		do over num;
			format num best6.2;
			num = 0.95*lag(num); /* first row of group 2 set to 95% of last row of previous group*/
		end;

	else do;
		do over num; /* remainder rows of 2nd group*/
			num = lag(num); /* all subsequent rows of group 2 equal to 1st row of group 2*/
		end;
	end;
end;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Feb 2025 19:07:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958087#M373958</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2025-02-03T19:07:44Z</dc:date>
    </item>
    <item>
      <title>Re: Array from first row of a group to be a fraction of the last row of the previous group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958089#M373960</link>
      <description>&lt;P&gt;Thanks, Tom&lt;BR /&gt;This kindaaa does what I'm trying to get to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to being "much more specific", I literally say what I'd like help with: "&lt;STRONG&gt;all rows of a second group&lt;/STRONG&gt; to be a fraction of the corresponding values in last row of the previous group". I don't say anything else because I don't want anything else.&lt;/P&gt;
&lt;P&gt;Admittedly, though, I create confusion by speaking in the topic title about the first row and then about all rows - it's just once I've figured how to change the first row I'll be able to change the rest of the rows too.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 19:43:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958089#M373960</guid>
      <dc:creator>RDzh</dc:creator>
      <dc:date>2025-02-03T19:43:10Z</dc:date>
    </item>
    <item>
      <title>Re: Array from first row of a group to be a fraction of the last row of the previous group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958091#M373961</link>
      <description>&lt;P&gt;Starting with&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Group $ val1 val2 ;
datalines;
1 5 8
1 4 10
1 7 11
2 7 1
2 5 9
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I'd like to get to&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  input Group $ val1 val2 ;
datalines;
1 5 8
1 4 10
1 7 11
2 6.95 10.45
2 6.95 10.45
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 03 Feb 2025 19:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958091#M373961</guid>
      <dc:creator>RDzh</dc:creator>
      <dc:date>2025-02-03T19:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Array from first row of a group to be a fraction of the last row of the previous group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958092#M373962</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/48576"&gt;@RDzh&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thanks, Tom&lt;BR /&gt;This kindaaa does what I'm trying to get to.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As to being "much more specific", I literally say what I'd like help with: "&lt;STRONG&gt;all rows of a second group&lt;/STRONG&gt; to be a fraction of the corresponding values in last row of the previous group". I don't say anything else because I don't want anything else.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;So that is much clearer than your first attempt.&amp;nbsp; But it does not cover what values you want for the first group.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You will need NEW variables to be able to do this.&amp;nbsp; You can always add some drop and/or rename statements to make them use the original names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For simplicity let's just do it for ONE variable first.&lt;/P&gt;
&lt;P&gt;Also let's assume you want the new variable to be MISSING on the first group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   by group;
   retain new1;
   output;
   if last.group then new1 = .95 * val1 ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;Obs    Group    val1    new1

 1       1      5.00     .
 2       1      4.00     .
 3       1      7.00     .
 4       2      6.65    6.65
 5       2      5.00    6.65
&lt;/PRE&gt;
&lt;P&gt;To extend it to multiple variables just add arrays.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by group;
  array old val1-val2;
  array new new1-new2;
  retain new1-new2;
  output;
  if last.group then do over old;
    new = old * 0.95;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want non-missing values for the first group then explain what values you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 19:46:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Array-from-first-row-of-a-group-to-be-a-fraction-of-the-last-row/m-p/958092#M373962</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-02-03T19:46:25Z</dc:date>
    </item>
  </channel>
</rss>

