I've found how to append a table to a base with proc append. I'm wondering if there is a simpler way (that doesn't require creating a temporary holding table) that adds a single record? All solutions I find require several lines, I'm wondering if there's something more straightforward like the "delete" command that I'm missing?
Adding a single record in SAS?
2k Views Asked by L Xandor AtThere are 3 best solutions below
On
As I think you have found, there are multiple options. If you have values you want to add, the one that makes the most sense from a lay person reading your code is a PROC SQL INSERT statement.
data test;
format a best. b $8. c date9.;
a = 1;
b = "ABC";
c = "01JAN2015"d;
run;
proc sql;
insert into test (b,c,a)
values ("DEF", "01JAN2014"d, 2);
quit;
The specification of the variable order (b,c,a) is optional. If it is not specified, then the values must be in the same order as the columns on the table.
On
You can use a data step with a modify statement to insert additional rows. Any other sort of data step involves creating a temporary file and replacing the original, which is undesirable when you're making a small change to a large dataset.
data class;
set sashelp.class;
run;
data class;
modify class;
Name = 'ZZZ';
output;
stop;
run;
This reads in the first row of the dataset to the PDV and then appends it with an updated value of Name, leaving the values of all the other variables as they were in the first row.
N.B. without the stop statement, this will cause an infinite loop, as SAS will alternately add and append records indefinitely, never getting any closer to the end of the dataset.
If you want to conditionally add observations from another dataset then you can use
outputto tell SAS to output the PDV as it stands:Generate example input datasets:
Code to add in single record:
Read in
source1and output the observation before testing the condition (i=5) to read in an observation fromsource2:However, it's important to know the interaction between the two
setstatements:Any variables that are in BOTH datasets will be overwritten by either
setstatement - so the value of this variable will be updated even if the previous value has not been output (see observation number 6 of thewantdataset)Any variables that are only in ONE dataset will effectively be 'retained' across observations until another
setstatement for the appropriate source dataset is encountered (see observations 7 to 11 of thewantdataset)For details of the PDV processing logic see here