How can i add extra data to an existing dataset???
data want;
set have(ID flag time);
ID=103; flag="Y" ;time="xxx "DT; ---------/*103 already exists in the have dataset and want to create more extra records*/
ID=105 ; flag="N" time="XX"DT; etc etc /*I am getting only the latest record????????
run;
Thanks
Just looking at the above code I believe you would need an explicit output statement after each new row you were adding to the dataset:
if end then do;
ID=105 ; flag="N" time="XX"DT;
output;
ID=103 ; flag="Y" time="XX"DT;
output;
ID=105 ; flag="Y" time="XX"DT;
output;
end;
EJ
PROC APPEND is your answer.
proc append base=have;
data=extra;
run;
Simply put all of your additional data into a new dataset with the same structures and field names. Use the FORCE option to add observations with new variables .
Hi
Please try the below code
i removed 103 record considering that the record already exists in the have data set, in order to insert the new records please include with the do and end statement as below
data want;
set have(ID flag time) end=eof;
output;
if end then do;
ID=105 ; flag="N" time="XX"DT;
/*include more records*/
output;
end;
run;
Thanks,
Jagadish
Just looking at the above code I believe you would need an explicit output statement after each new row you were adding to the dataset:
if end then do;
ID=105 ; flag="N" time="XX"DT;
output;
ID=103 ; flag="Y" time="XX"DT;
output;
ID=105 ; flag="Y" time="XX"DT;
output;
end;
EJ
Yes Jack, this is the right approach
Thank you
Hi,
Is there a semicolon after the flag variable too like shown below????in your code the semicolon is missing after the flag variable
Thanks
if end then do;
ID=105 ; flag="N"; time="XX"DT;
output;
ID=103 ; flag="Y"; time="XX"DT;
output;
ID=105 ; flag="Y"; time="XX"DT;
output;
end;
That is correct. Sorry did not notice the omission.
EJ
Alternately to proc append, there is proc sql insert method or the tried and true data step set. If its a large dataset append and insert have some advantages not having to rewrite the old data to the dataset,. Append has simplier syntax especially for very wide datasets.
Either way you need a separate dataset with the additions. Below I use Append to add to the original dataset and a data step to show an example of creating a new dataset with all the observations.
-----------------------------
data have;
format id $3. Flag $1. time timeampm8.;
infile cards dsd ;
input id flag $ time : time8.;
cards;
103,N,12:15 AM
105,Y,1:35 PM
;
run;
data Add;
format id $3. Flag $1. time timeampm8.;
infile cards dsd ;
input id flag $ time : time8.;
cards;
103,Y,2:15 PM
105,N,1:45 AM
;
run;
data want;
set have add;
run;
proc append base=have data=add force; run;
--------------------------
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.