BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10

Hi 

Here I want to insert row customize  for below example i want output all rows along with new insert observation 

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;
7 REPLIES 7
Patrick
Opal | Level 21

If I understand the question then a 2nd OUTPUT right after the SET statement should do the job.

data row;
  set custom_insert;
  output;

  if _n_=3 then
    do;
      name='Sachin';
      sex= 'f';
      age=22;
      height=12;
      weight=35;
      output;
    end;
run;
BrahmanandaRao
Lapis Lazuli | Level 10

Thank You Patick

If we insert very first row the dataset  how can we achieve i want Sachin would be first record

sbxkoenk
SAS Super FREQ

Hello,

 

Do you want this?

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;

 

Koen

data_null__
Jade | Level 19
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;

Capture.PNG

Patrick
Opal | Level 21

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.

But with SAS: Below one option.

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;
mkeintz
PROC Star

@Patrick wrote:

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.

I beg to differ.

 

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.

 

And being conscious of data as sequential observations predates "Excel thinking" by decades.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
mkeintz
PROC Star

Here is a slight generalization of what you've been offered:

 

%let new_row=23;

data want;
  if 0 then set sashelp.class  nobs=nrecs;
  if _n_=min(&new_row,nrecs+1) then do;
    name='Sachin';
    sex= 'f';
    age=22;
    height=12;
    weight=35;
    output;
  end;
  set sashelp.class;
  output;
run;

The "if 0 then set ..." statement is just to have SAS establish the variable attributes.  This avoids an unintentional attribute established in the DO group.

 

The "if _n_= " expression test for desired row number.  If the desired row number exceeds the number of incoming observations, then the new record is appended at the end of the dataset.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 4458 views
  • 8 likes
  • 5 in conversation