BookmarkSubscribeRSS Feed
mgrasmussen
Quartz | Level 8

Dear SAS experts

 

I want to insert a new observation after "James" (3rd observation) which includes missing data for all variables except for Sex to which I want to assign the value "T1" (just as an example). One way of doing it is:

 

*Inserting a new observation after _N_=3 (after "James") in the dataset;

data test;
length Sex $2;
set sashelp.class;
output;
if _N_ =3 then do;
Sex="T1"; /* Only place to insert non-missing data. Missing data for the rest of the variables. */
Name=".";
Age=.;
Height=.;
Weight=.;
output;
end;
run;

 

My questions are:

 

- Instead of basing the insert of the new observation on _N_=3 could I somehow simply state that an observation should be introduced after Name="James"?

 

- Above I write that each of the remaining variables should be "." or .. Could I write this in a more simple way, i.e. that all remaining variables should be missing (or "." when referring to character variables)? This is especially relevant in cases where the above procedure should be performed in dataset which contain many variables (not just 5 as in class).

 

Thank you 

 

Best regards,

 

Martin

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Is this what you want?

 

data test;
   set sashelp.class;
   output;

   if Name = "James" then do;
      call missing (of _ALL_);
      sex = "T1";
      output;
   end;
run;
mgrasmussen
Quartz | Level 8
Dear Peter

Perfect, yes - this is what I was looking for.

Can I do it multiple times in the same data set? Could I e.g. perform the following operation - "Jeffrey" and T2 - at the same time:

data test;
set sashelp.class;
output;

if Name = "Jeffrey" then do;
call missing (of _ALL_);
sex = "T2";
output;
end;
run;

Thank you
Patrick
Opal | Level 21

This must be an academic problem. If the order of observations is important then you need to sort the table after inserting new obs. That's the only way SAS will "know" that the table is sorted.

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

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
  • 3 replies
  • 871 views
  • 0 likes
  • 3 in conversation