<?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: Insert Nth row in existing dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801667#M315518</link>
    <description>&lt;P&gt;Here is a slight generalization of what you've been offered:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let new_row=23;

data want;
  if 0 then set sashelp.class  nobs=nrecs;
  if _n_=min(&amp;amp;new_row,nrecs+1) then do;
    name='Sachin';
    sex= 'f';
    age=22;
    height=12;
    weight=35;
    output;
  end;
  set sashelp.class;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "if 0 then set ..." statement is just to have SAS establish the variable attributes.&amp;nbsp; This avoids an unintentional attribute established in the DO group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "if _n_= " expression test for desired row number.&amp;nbsp; If the desired row number exceeds the number of incoming observations, then the new record is appended at the end of the dataset.&lt;/P&gt;</description>
    <pubDate>Fri, 11 Mar 2022 14:14:50 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2022-03-11T14:14:50Z</dc:date>
    <item>
      <title>Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801623#M315494</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here I want to insert row customize&amp;nbsp; for below example i want output all rows along with new insert observation&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data custom_insert;
set sashelp.class;
run;


data row;
set custom_insert;
if _n_=3 then do 
name='Sachin';
sex= 'f';
age=22;
height=12;
weight=35;
output;
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Mar 2022 09:18:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801623#M315494</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2022-03-11T09:18:27Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801625#M315495</link>
      <description>&lt;P&gt;If I understand the question then a 2nd OUTPUT right after the SET statement should do the job.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data row;
  set custom_insert;
  output;

  if _n_=3 then
    do;
      name='Sachin';
      sex= 'f';
      age=22;
      height=12;
      weight=35;
      output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 11 Mar 2022 09:49:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801625#M315495</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-11T09:49:53Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801627#M315497</link>
      <description>&lt;P&gt;Thank You Patick&lt;/P&gt;
&lt;P&gt;If we insert very first row the dataset&amp;nbsp; how can we achieve i want Sachin would be first record&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 10:00:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801627#M315497</guid>
      <dc:creator>BrahmanandaRao</dc:creator>
      <dc:date>2022-03-11T10:00:26Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801628#M315498</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you want this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data custom_insert;
set sashelp.class;
run;

data row;
set custom_insert;
 if _n_=1 then do;
  output;
  name='Sachin';
  sex= 'f';
  age=22;
  height=12;
  weight=35;
end;
output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 10:36:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801628#M315498</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2022-03-11T10:36:00Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801665#M315516</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data custom_insert;
   set sashelp.class;
   run;

data newrow;
   if 0 then set custom_insert;
   name='Sachin';
   sex= 'f';
   age=22;
   height=12;
   weight=35;
   output;
   stop;
   run;

data row;
   set newrow custom_insert;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 247px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/69401i0F3F434118948067/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 14:05:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801665#M315516</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2022-03-11T14:05:06Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801667#M315518</link>
      <description>&lt;P&gt;Here is a slight generalization of what you've been offered:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let new_row=23;

data want;
  if 0 then set sashelp.class  nobs=nrecs;
  if _n_=min(&amp;amp;new_row,nrecs+1) then do;
    name='Sachin';
    sex= 'f';
    age=22;
    height=12;
    weight=35;
    output;
  end;
  set sashelp.class;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The "if 0 then set ..." statement is just to have SAS establish the variable attributes.&amp;nbsp; This avoids an unintentional attribute established in the DO group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "if _n_= " expression test for desired row number.&amp;nbsp; If the desired row number exceeds the number of incoming observations, then the new record is appended at the end of the dataset.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 14:14:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801667#M315518</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-11T14:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801802#M315588</link>
      <description>&lt;P&gt;The row where you insert a record shouldn't matter. If it where a database table then the concept of a row number wouldn't even exist. This is "Excel thinking" which you better get rid-off when working with SAS or any database.&lt;/P&gt;
&lt;P&gt;But with SAS: Below one option.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data row;
  if 0 then set sashelp.class;
  if _n_=1 then
    do;
      name='Sachin';
      sex= 'f';
      age=22;
      height=12;
      weight=35;
      output;
    end;
  set sashelp.class;
  output;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 12 Mar 2022 11:24:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801802#M315588</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2022-03-12T11:24:47Z</dc:date>
    </item>
    <item>
      <title>Re: Insert Nth row in existing dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801815#M315592</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The row where you insert a record shouldn't matter. If it where a database table then the concept of a row number wouldn't even exist. This is "Excel thinking" which you better get rid-off when working with SAS or any database.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I beg to differ.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any DATA step or PROC step that uses a BY statement almost always is much more efficient if the "rows" are sorted (as opposed to indexed) by the BY variables. Historical data, time series, any programming that depends on use of the LAG function, FIRSTOBS or OBS parameters, or POINT= options will depend on users being aware of row locations.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And being conscious of data as sequential observations predates "Excel thinking" by decades.&lt;/P&gt;</description>
      <pubDate>Sat, 12 Mar 2022 15:37:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Insert-Nth-row-in-existing-dataset/m-p/801815#M315592</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-03-12T15:37:33Z</dc:date>
    </item>
  </channel>
</rss>

