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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 542 views
  • 0 likes
  • 3 in conversation