<?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: Add a new row before last row for each group in SAS data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886121#M350161</link>
    <description>&lt;P&gt;No blue rows appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you explain the logic used in determining where the rows should go? The very brief description in the title doesn't seem to match what I see in your desired output. Also, you use the word "group" but don't define it.&lt;/P&gt;</description>
    <pubDate>Mon, 24 Jul 2023 17:12:04 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2023-07-24T17:12:04Z</dc:date>
    <item>
      <title>Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886120#M350160</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;I have the following data set:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input subjid visit$ aval;
cards;
101 screen 20
&lt;FONT color="#0000FF"&gt;101 base   25&lt;/FONT&gt;
101 v1     25
101 v2     26
102 screen 25
&lt;FONT color="#0000FF"&gt;102 base   30&lt;/FONT&gt;
102 v1     25
102 v2     30
103 screen 23
&lt;FONT color="#0000FF"&gt;103 base   33&lt;/FONT&gt;
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want to have the above highlighted rows in &lt;FONT color="#0000FF"&gt;blue color&amp;nbsp;&lt;FONT color="#000000"&gt;inserted as below:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
	input subjid visit$ aval;
cards;
101 screen 20
101 base   25
101 v1     25
&lt;FONT color="#FF00FF"&gt;101 base   25&lt;/FONT&gt;
101 v2     26
102 screen 25
102 base   30
102 v1     25
&lt;FONT color="#FF00FF"&gt;102 base   30&lt;/FONT&gt;
102 v2     30
103 screen 23
103 base   33
103 v1     20
&lt;FONT color="#FF00FF"&gt;103 base   33&lt;/FONT&gt;
103 v2     25
;
run;	&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Can anyone help?&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Thanks&lt;/DIV&gt;</description>
      <pubDate>Mon, 24 Jul 2023 17:06:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886120#M350160</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-07-24T17:06:30Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886121#M350161</link>
      <description>&lt;P&gt;No blue rows appear.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you explain the logic used in determining where the rows should go? The very brief description in the title doesn't seem to match what I see in your desired output. Also, you use the word "group" but don't define it.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 17:12:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886121#M350161</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-24T17:12:04Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886124#M350163</link>
      <description>&lt;P&gt;Here, is the data set that I have:&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input subjid visit$ aval;&lt;BR /&gt;cards;&lt;BR /&gt;101 screen 20&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;101 base 25&lt;/FONT&gt;&lt;BR /&gt;101 v1 25&lt;BR /&gt;101 v2 26&lt;BR /&gt;102 screen 25&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;102 base 30&lt;/FONT&gt;&lt;BR /&gt;102 v1 25&lt;BR /&gt;102 v2 30&lt;BR /&gt;103 screen 23&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;103 base 33&lt;/FONT&gt;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are 3 subjid's 101, 102 and 103. For each of these subjid group values, the row with visit as base is highlighted in &lt;FONT color="#0000FF"&gt;blue&lt;/FONT&gt; color.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the above blue color highlighted rows inserted in between within each subjid group as follows and for subjid 103 since there are no rows for visits v1 and v2 and hence need to add these rows also:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;data want;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt; input subjid visit$ aval;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;cards;&lt;/DIV&gt;
&lt;DIV&gt;101 screen 20&lt;/DIV&gt;
&lt;DIV&gt;101 base&amp;nbsp; &amp;nbsp;25&lt;/DIV&gt;
&lt;DIV&gt;101 v1&amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT color="#FF9900"&gt;101 base&amp;nbsp; &amp;nbsp;25&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;101 v2&amp;nbsp; &amp;nbsp; &amp;nbsp;26&lt;/DIV&gt;
&lt;DIV&gt;102 screen 25&lt;/DIV&gt;
&lt;DIV&gt;102 base&amp;nbsp; &amp;nbsp;30&lt;/DIV&gt;
&lt;DIV&gt;102 v1&amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT color="#FF9900"&gt;102 base&amp;nbsp; &amp;nbsp;30&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;102 v2&amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/DIV&gt;
&lt;DIV&gt;103 screen 23&lt;/DIV&gt;
&lt;DIV&gt;103 base&amp;nbsp; &amp;nbsp;33&lt;/DIV&gt;
&lt;DIV&gt;103 v1&amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT color="#FF9900"&gt;103 base&amp;nbsp; &amp;nbsp;33&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;103 v2&amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/DIV&gt;
&lt;DIV&gt;;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;Then newly inserted rows are highlighted in &lt;FONT color="#FF9900"&gt;Amber&lt;/FONT&gt; color.&lt;/DIV&gt;
&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV&gt;Thanks&lt;/DIV&gt;</description>
      <pubDate>Mon, 24 Jul 2023 17:24:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886124#M350163</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-07-24T17:24:43Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886125#M350164</link>
      <description>&lt;P&gt;Okay, I think I see a pattern in the output; but I &lt;EM&gt;really really really&lt;/EM&gt; want you to tell me what the pattern is and what the logic is; instead of me trying to figure it out (and possibly getting it wrong). And you have not said what determines where these extra rows will be added.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please tell us what you know, that is the best way to get the help you need. Please don't make us guess, please don't make us try to figure it out, we could possibly get it wrong and that is not a good way to get the help you need. (And this applies not only to this question, but to all questions you ask in the future, please tell us what you know, don't make us guess, and don't make us ask for the logic or details to be explained).&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 17:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886125#M350164</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-07-24T17:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886128#M350166</link>
      <description>&lt;P&gt;It's that I want to insert a row containing the base value for subjid before their visit 2 i.e., v2. I am just checking how to carry forward this row and insert it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If visit 1 and visit 2 are missing for any subjid (in this example, they are missing for subjid 103), then create these two rows for that subjid (here aval can have any value as it is just for test purpose) and insert the row with base visit before the row of visit 2.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 17:45:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886128#M350166</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-07-24T17:45:33Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886174#M350190</link>
      <description>&lt;P&gt;In dataset WANT, for subjid 103, you put a V! record with aval=20 and a V2 record with aval=25, but where did these aval values come from in your dataset HAVE?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The process for subjid 101 and 102 is clear, but not for 103.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Jul 2023 22:30:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886174#M350190</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-07-24T22:30:50Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886180#M350193</link>
      <description>&lt;P&gt;For 103 v1 and v2, since it's just for testing purpose, I have used some example values for aval.&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jul 2023 04:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886180#M350193</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-07-25T04:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886185#M350195</link>
      <description>&lt;P&gt;If anyone can help me to achieve the following, would be of great help. In this, I have excluded the requirement for subjid 103 v1 and v2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input subjid visit$ aval;&lt;BR /&gt;cards;&lt;BR /&gt;101 screen 20&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;101 base 25&lt;/FONT&gt;&lt;BR /&gt;101 v1 25&lt;BR /&gt;101 v2 26&lt;BR /&gt;102 screen 25&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;102 base 30&lt;/FONT&gt;&lt;BR /&gt;102 v1 25&lt;BR /&gt;102 v2 30&lt;BR /&gt;103 screen 23&lt;BR /&gt;&lt;FONT color="#000000"&gt;103 base 33&lt;/FONT&gt;&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There are 3 subjid's 101, 102 and 103. For subjid 101 and 102, the row with visit as base is highlighted in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT color="#0000FF"&gt;blue&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;color. Since, subjid 103 does not have v1 and v2, I have not highlighted the base row is not highlighted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want the above blue color highlighted rows inserted in between within each subjid group as follows:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV&gt;data want;&lt;/DIV&gt;
&lt;DIV&gt;&lt;SPAN&gt;input subjid visit$ aval;&lt;/SPAN&gt;&lt;/DIV&gt;
&lt;DIV&gt;cards;&lt;/DIV&gt;
&lt;DIV&gt;101 screen 20&lt;/DIV&gt;
&lt;DIV&gt;101 base&amp;nbsp; &amp;nbsp;25&lt;/DIV&gt;
&lt;DIV&gt;101 v1&amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT color="#FF9900"&gt;101 base&amp;nbsp; &amp;nbsp;25&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;101 v2&amp;nbsp; &amp;nbsp; &amp;nbsp;26&lt;/DIV&gt;
&lt;DIV&gt;102 screen 25&lt;/DIV&gt;
&lt;DIV&gt;102 base&amp;nbsp; &amp;nbsp;30&lt;/DIV&gt;
&lt;DIV&gt;102 v1&amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;/DIV&gt;
&lt;DIV&gt;&lt;FONT color="#FF9900"&gt;102 base&amp;nbsp; &amp;nbsp;30&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV&gt;102 v2&amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/DIV&gt;
&lt;DIV&gt;103 screen 23&lt;/DIV&gt;
&lt;DIV&gt;103 base&amp;nbsp; &amp;nbsp;33&lt;/DIV&gt;
&lt;DIV&gt;;&lt;/DIV&gt;
&lt;DIV&gt;run;&lt;/DIV&gt;
&lt;DIV&gt;Then newly inserted rows are highlighted in&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;FONT color="#FF9900"&gt;Amber&lt;/FONT&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;color.&lt;/DIV&gt;</description>
      <pubDate>Tue, 25 Jul 2023 06:37:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886185#M350195</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-07-25T06:37:18Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886187#M350197</link>
      <description>&lt;P&gt;I have tried the following but, it is inserting a new row for subjid 101 only and not for subjid 102. But, I want to insert a new record for subjid 102 also.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the code that I have tried:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
	set visit_sorted;
	by subjid visitn;
	output;

	retain _basesubjid _basevisit _baseaval _basevisitn;

	if visit = 'base' and aval ne . then do;
		_basesubjid = subjid;
		_basevisit  = visit;
		_baseaval   = aval;
		_basevisitn = visitn;
	end;

	if _n_= 3 then do;
		subjid = _basesubjid;
		visit = _basevisit;
		aval  = _baseaval;
		visitn = _basevisitn;
		dtype = 'BOCF';
		output;
	end;
drop _basesubjid _basevisit _baseaval _basevisitn;
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If anyone can help to insert the row for subjid 102 record also, it would be of great help. For subjid 102, following record highlighted in '&lt;FONT color="#FF0000"&gt;Red&lt;/FONT&gt;' color to be inserted between v1 and v2 records.&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input subjid visit$ aval;&lt;BR /&gt;cards;&lt;BR /&gt;101 screen 20&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;101 base 25&lt;/FONT&gt;&lt;BR /&gt;101 v1 25&lt;BR /&gt;101 v2 26&lt;BR /&gt;102 screen 25&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;102 base 30&lt;/FONT&gt;&lt;BR /&gt;102 v1 25&lt;BR /&gt;102 v2 30&lt;BR /&gt;103 screen 23&lt;BR /&gt;103 base 33&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Jul 2023 07:28:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886187#M350197</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-07-25T07:28:58Z</dc:date>
    </item>
    <item>
      <title>Re: Add a new row before last row for each group in SAS data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886218#M350201</link>
      <description>&lt;P&gt;By hardcoding for v1, I could get the desire result and here is the code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have1;
	set visit_sorted;
	by subjid visitn;
	output;

	retain _basesubjid _basevisit _baseaval _basevisitn;

	if visit = 'base' and aval ne . then do;
		_basesubjid = subjid;
		_basevisit  = visit;
		_baseaval   = aval;
		_basevisitn = visitn;
	end;

/*	if _n_= 3 then do;*/
	if visit = 'v1' then do;
		subjid = _basesubjid;
		visit = _basevisit;
		aval  = _baseaval;
		visitn = _basevisitn;
		dtype = 'BOCF';
		output;
	end;
drop _basesubjid _basevisit _baseaval _basevisitn;
run;

proc print;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If there is any other better way to achieve this, please let me know.&amp;nbsp; Actually, I want know if there is any other way to achieve without hardcoding as above.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result of the above code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

Obs subjid visit aval visitn dtype 
1 101 screen 20 1   
2 101 base 25 2   
3 101 v1 25 3   
4 101 base 25 2 BOCF 
5 101 v2 26 4   
6 102 screen 25 1   
7 102 base 30 2   
8 102 v1 25 3   
9 102 base 30 2 BOCF 
10 102 v2 30 4   
11 103 screen 23 1   
12 103 base 33 2   


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Jul 2023 11:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Add-a-new-row-before-last-row-for-each-group-in-SAS-data-step/m-p/886218#M350201</guid>
      <dc:creator>Moksha</dc:creator>
      <dc:date>2023-07-25T11:41:28Z</dc:date>
    </item>
  </channel>
</rss>

