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
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;
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.